通常 DFA 用于检查给定的字符串是否以某种语言存在。例如 _ab1c 存在于 C 中的变量语言中。
我在做什么? 但正如这个问题中所述,我正在使用 DFA 来跟踪所有评论、字符串等。
我过得怎么样? 考虑一个在给定字符串/程序中跟踪 //comments 的示例。
static int makeTransition[][] = {
/* Transition Table */
/*{other,\n, \, /, *, ', "} */
/*q0*/ { 0, 0, 0, 1, 0, 0, 0},
/*q1*/ { 0, 0,-1, 2, 0, 0, 0},
/*q2*/ { 2, 0, 2, 2, 2, 2, 2},
};
为此,如果我有,
void assignPointerValuesInPairs(int index)
{
/*comments is an ArrayList
before marking start hotpointer = -1
after marking start hotpointer = 0
after marking end hotpointer is resetted to -1*/
switch(currentState)
{
case 2: /*q2*/
comments.add(/*mark start*/);
hotPointer = 0;
break;
case 0: /*On initial state q0*/
switch(hotPointer)
{
case 0: //If I am in end of comment.
comments.add(/*mark end*/);
hotPointer = -1; //Resetting the hotPointer.
break;
case -1: /*Already in q1 only*/
/*Do nothing*/
}
}
}
public static void traceOut(String s) //entire program is accepted as string.
{
int index = 0;
while (index < s.length() ) {
char c = s.charAt(index);
try{
currentState = makeTransition[currentState][symbolToInteger(c)];
if(currentState == -1)
throw new InvalidSyntaxException();
}
catch(InvalidSyntaxException e){
currentState = 0;
invalidSyntax.add(index);
}
assignPointerValuesInPairs(index);
index++;
}
currentState = 0;
assignPointerValuesInPairs(index); //These 2 statements help to color while typing.. (i.e) It forces the current state to get finished abruptly.
}
}
我的问题是...
我可以使用 DFA,以这种方式标记 //comment 的结束和开始,或者我必须遵循 CFG 等其他方式。
IE
我的声明:我可以使用 DFA,不仅可以检查特定语言,还可以跟踪给定字符串中属于某些语言的某些字符串。(证明:通过上述方法)。
我的上述说法正确吗?