通常,DFA用於檢查給定的字符串是否以某種語言存在。 例如_ab1c存在於C中的變量語言中。我可以使用DFA來跟蹤某些語言的字符串嗎?
我在做什麼? 但正如this question說,我使用的DFA跟蹤所有註釋,字符串等
如何我做的? 考慮在給定的字符串/程序中跟蹤//註釋的示例。
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,以紀念結束,在此//註釋的開始這樣, 或我也要跟着喜歡CFG等其他方式..
即
我的聲明:我可以使用DFA,不僅可以檢查特定的語言,還可以 在給定的 字符串中跟蹤某些字符串屬於某些語言。 (證明:按上述方法)。
我的上述陳述是否正確?
+1「但是,DFA會非常大以至於無法實現。」這條線清除了我的懷疑。 –
我還有一個疑問。正如你所說,我已經以這種方式實現了,我的DFA非常龐大。我正在撰寫DFA以識別關鍵字。我可以將它合併到單個DFA中(狀態將減少)還是可以爲此編寫單獨的DFA(需要更多狀態)。我必須看到什麼?沒有狀態或可讀性? - –