编辑: 戴静菡 | 2019-07-17 |
case '
}'
: insertToken(5, -1);
printf( %c\t界限符\n ,ch);
break;
case '
;
'
: insertToken(6, -1);
printf( %c\t界限符\n ,ch);
break;
case '
,'
: insertToken(7, -1);
printf( %c\t界限符\n ,ch);
break;
case '
='
: insertToken(8, -1);
printf( %c\t运算符\n ,ch);
break;
case '
+'
: insertToken(9, -1);
printf( %c\t运算符\n ,ch);
break;
case '
*'
: insertToken(10, -1);
printf( %c\t运算符\n ,ch);
break;
case '
('
: insertToken(11, -1);
printf( %c\t界限符\n ,ch);
break;
case '
)'
: insertToken(12, -1);
printf( %c\t界限符\n ,ch);
break;
default:break;
} } 语法分析 (1).文法规则 K->
main ( ) { P } P->
P M S P->
D M->
epsilon D->
T L ;
T->
int L->
L , id L->
id S->
id = E ;
E->
E + E E->
E * E E->
( E ) E->
id E->
const (2)action和goto表Action表如下: 1001,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,1002,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,1003,0, 0,0,0,0,1004,0,0,0,0,0,0,0,0,0, 0,1008,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,4,0,1009,0,0,0,0,0,0,0,0, 0,0,0,3,0,3,0,0,0,0,0,0,0,0, 0,0,0,1012,0,0,0,0,0,0,0,0,0,0, 0,0,0,6,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,-1, 0,0,0,1014,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1015,1016,0,0,0,0,0,0, 0,0,0,0,0,0,8,8,0,0,0,0,0,0, 0,0,0,2,0,2,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1017,0,0,0,0,0, 0,0,0,5,0,5,0,0,0,0,0,0,0,0, 0,0,0,1018,0,0,0,0,0,0,0,0,0,0, 0,0,1022,1021,0,0,0,0,0,0,0,1020,0,0, 0,0,0,0,0,0,7,7,0,0,0,0,0,0, 0,0,0,0,0,0,1023,0,0,1024,1025,0,0,0, 0,0,1022,1021,0,0,0,0,0,0,0,1020,0,0, 0,0,0,0,0,0,13,0,0,13,13,0,13,0, 0,0,0,0,0,0,14,0,0,14,14,0,14,0, 0,0,0,9,0,9,0,0,0,0,0,0,0,0, 0,0,1022,1021,0,0,0,0,0,0,0,1020,0,0, 0,0,1022,1021,0,0,0,0,0,0,0,1020,0,0, 0,0,0,0,0,0,0,0,0,1024,1025,0,1029,0, 0,0,0,0,0,0,10,0,0,10,1025,0,10,0, 0,0,0,0,0,0,11,0,0,11,11,0,11,0, 0,0,0,0,0,0,12,0,0,12,12,0,12,0 Goto表如下: 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 5,0,6,0,7,0,0, 0,0,0,10,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,11,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,13,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,19, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,26, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,27, 0,0,0,0,0,0,28, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0 (3).主要代码: void Analyse(Stack stateStack,Stack termStack,Stack tokenStack) { int temp, j, k;
initize();
printf( \n\n\n-语法分析-n );
position = headToken;
Push(13,stateStack);
Push(-1,tokenStack);
Push(0,termStack);
while(actionTable[Top(termStack)][position->
id] != -1) { temp = actionTable[Top(termStack)][position->
id];
if(temp == 0) return;
else { if(temp/1000 != 0) { char* c = NULL;
c = isTerminative(position->
id);
Push(position->
id,stateStack);
Push(temp%1000,termStack);
Push(position->
entry,tokenStack);
position = position->
next;
} else { panner(temp,tokenStack);
if(!empty(temp)) { for(j = 0;
j <
grammar[temp].length;
j++) { Pop(stateStack);
Pop(termStack);
Pop(tokenStack);
} } k = isNonTerminative(grammar[temp].left);
Push(k,stateStack);
Push(-1,tokenStack);
j = Top(termStack);
Push(gotoTable[j][k],termStack);
} } } } 其中所涉及的堆栈代码见源........