Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找在fortran源文件中计算变量的位置_C#_Fortran_Fortran77 - Fatal编程技术网

C# 查找在fortran源文件中计算变量的位置

C# 查找在fortran源文件中计算变量的位置,c#,fortran,fortran77,C#,Fortran,Fortran77,我正试图用C#编写一个程序,从数据库中获取一个字符串表(变量名),并搜索一个包含30000个Fortran 77源文件的目录,以确定该变量的计算位置。这些变量通常仅在一个fortran文件中计算一次,但在其他文件中使用多次。数据库表中的变量都是在fortran文件中的某个地方显式定义的。到目前为止,我已经完成了大部分工作,首先构建了每个变量出现的文件列表,然后逐行搜索该列表中的文件。我一直在寻找变量出现在“=”符号的哪一侧,方法如下: CompareInfo ci = CultureInfo.C

我正试图用C#编写一个程序,从数据库中获取一个字符串表(变量名),并搜索一个包含30000个Fortran 77源文件的目录,以确定该变量的计算位置。这些变量通常仅在一个fortran文件中计算一次,但在其他文件中使用多次。数据库表中的变量都是在fortran文件中的某个地方显式定义的。到目前为止,我已经完成了大部分工作,首先构建了每个变量出现的文件列表,然后逐行搜索该列表中的文件。我一直在寻找变量出现在“=”符号的哪一侧,方法如下:

CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
for (int k = 0; k < fullpaths.Count; k++)
{
    string line;
    // Read the file and display it line by line.
    System.IO.StreamReader FortranFile = new System.IO.StreamReader(fullpaths[k]);
    while ((line = FortranFile.ReadLine()) != null)
    {
        // Search the file line-by-line for the variable
        if (ci.IndexOf(line, Variable, CompareOptions.IgnoreCase) > 0)
        {
            // Search for the equals sign
            int equalLocation = ci.IndexOf(line, "=");
            if (equalLocation > 0)
            {
                // substring LHS
                string subLineLHS = line.Substring(0, equalLocation+1);
                // is the line commented out?
                if (Convert.ToString(subLineLHS[0]) == "C" ||
                    Convert.ToString(subLineLHS[0]) == "!" ||
                    Convert.ToString(subLineLHS[0]) == "c" ||
                    Convert.ToString(subLineLHS[0]) == "*")
                {
                    continue;
                }
                // ignore if the line contains a DO, IF, or WHILE loop, 
                // to prevent reading IF [Variable] = xxxx as being calculated.
             else if ( (ci.IndexOf(subLineLHS, "IF", CompareOptions.IgnoreCase) > 0) ||
                       (ci.IndexOf(subLineLHS, "DO", CompareOptions.IgnoreCase) > 0) ||
                      (ci.IndexOf(subLineLHS, "WHILE", CompareOptions.IgnoreCase) > 0))
                {
                    continue;
                }
                // find where the variable is used in the line
            else if (ci.IndexOf(subLineLHS, Variable, CompareOptions.IgnoreCase)  > 0 )
                {
                    isCalculated[k] = true;
                    calculatedLine[k] = counter;
                }
            }
        } //if loop
        counter++;
    } //while loop

    FortranFile.Close();
}
这个方法会告诉我变量是在“variable=xx”这一行计算的。单行if语句,例如if(something)variable=xx也被忽略。带有多个=符号的线条也可能给我带来问题

有什么建议可以让我避开这个问题吗?有没有更好的方法?请对我宽容点,我不是程序员


谢谢

最能防止错误的方法是解析Fortran代码并从语法树开始工作

我的建议:使用。例如,见;信息技术
ctags
生成一组源代码文件中所有命名实体的索引。索引存储在可以从大多数文件编辑器/IDE读取的数据结构(标记)中。 如果在您喜爱的文本编辑器中导入该标记文件,则当您将光标定位在变量定义上并采取适当的操作时,将能够跳转到该变量定义

标签文件也很容易读取和解析:它的结构如下

named_entity<Tab>file_where_it_is_defined<Tab>location_in_the_file

我们可以观察到
gpar
变量是在
remlf90.f90
中定义的,
hashia1
是在
。/libs/sparse2.f
等中定义的。

如果你的源代码说
如果(something=xx和
,那么它就错了。在Fortran中,Equals使用
.EQ.
=
而“and”应该是
。和。
谢谢你的回复…那一行基本上是伪代码。fortran源文件很好。我在那里描述的问题是,在多行上继续的公式没有用我的c程序正确解析。你是被迫使用c吗?我想
grep
会工作得更快。不,我不是,谢谢你知道除了显式赋值外,还有很多方法可以设置变量。
named_entity<Tab>file_where_it_is_defined<Tab>location_in_the_file
gpar    remlf90.f90 /^           xrank,npar,gpar,/;"    v   program:REMLF90
hashia1 ../libs/sparse2.f   /^      subroutine hashia1(/;"  s
hashv1  ../libs/sparse3.f   /^      integer function hashv1(/;" f
hashvr_old  ../libs/sparse2.f   /^      integer function hashvr_old(/;" f