Commit 8db9c823 authored by cwzsquare's avatar cwzsquare
Browse files

"完善char处理" Fixed

parent 4a34b303
...@@ -49,7 +49,7 @@ TERMINAL nextToken() ...@@ -49,7 +49,7 @@ TERMINAL nextToken()
state=LexTable[state][LEX_ADDMIN]; state=LexTable[state][LEX_ADDMIN];
else if (c>='0' && c<='9') else if (c>='0' && c<='9')
state=LexTable[state][LEX_DIGIT]; state=LexTable[state][LEX_DIGIT];
else if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c=='_')) else if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c=='_')||(c=='\\')||(c=='\"')||(c=='\'')) //注意对转义字符的支持
state=LexTable[state][LEX_LETTER_]; state=LexTable[state][LEX_LETTER_];
else if (c=='(' || c==')' || c=='{' || c=='}' || c==',' || c==';') else if (c=='(' || c==')' || c=='{' || c=='}' || c==',' || c==';')
state=LexTable[state][LEX_SYMBOL]; state=LexTable[state][LEX_SYMBOL];
...@@ -75,7 +75,8 @@ TERMINAL nextToken() ...@@ -75,7 +75,8 @@ TERMINAL nextToken()
break; break;
case 105: tokenStr[tokenLen]='\0'; case 105: tokenStr[tokenLen]='\0';
token.token=FoundKeyword(); token.token=FoundKeyword();
token.tokenVal.str=tokenStr; if (token.token == SYN_CH) token.tokenVal.number=tokenStr[1];
else token.tokenVal.str=tokenStr;
break; break;
case 201: if (feof(sFile)) case 201: if (feof(sFile))
{// printf("Meet file end!\n"); {// printf("Meet file end!\n");
...@@ -153,6 +154,7 @@ static int STR2INT() ...@@ -153,6 +154,7 @@ static int STR2INT()
static int FoundKeyword() static int FoundKeyword()
{ {
if (tokenStr[0]=='\'' || tokenStr[0]=='\"') return(SYN_CH);
if (strcompare(tokenStr,"TRUE")) return(SYN_TRUE); if (strcompare(tokenStr,"TRUE")) return(SYN_TRUE);
if (strcompare(tokenStr,"FALSE")) return(SYN_FALSE); if (strcompare(tokenStr,"FALSE")) return(SYN_FALSE);
if (strcompare(tokenStr,"int")) return(SYN_INT); if (strcompare(tokenStr,"int")) return(SYN_INT);
......
...@@ -46,6 +46,8 @@ void SyntaxAnalysis() ...@@ -46,6 +46,8 @@ void SyntaxAnalysis()
printf("LEX: %d,%d\n",token.token,token.tokenVal.number); printf("LEX: %d,%d\n",token.token,token.tokenVal.number);
else if (token.token==SYN_ID) else if (token.token==SYN_ID)
printf("LEX: %d,%s\n",token.token,token.tokenVal.str); printf("LEX: %d,%s\n",token.token,token.tokenVal.str);
else if (token.token==SYN_CH) //既然要加上char支持,debug当然也要加上
printf("LEX: %d,%s\n",token.token,token.tokenVal.number);
else else
printf("LEX: %d\n",token.token); printf("LEX: %d\n",token.token);
token=nextToken(); token=nextToken();
...@@ -68,6 +70,8 @@ static int match (int t) ...@@ -68,6 +70,8 @@ static int match (int t)
curtoken_num=lookahead.tokenVal.number; curtoken_num=lookahead.tokenVal.number;
else if (t==SYN_ID) else if (t==SYN_ID)
for (p=lookahead.tokenVal.str,q=curtoken_str;(*q=*p)!='\0';p++,q++); for (p=lookahead.tokenVal.str,q=curtoken_str;(*q=*p)!='\0';p++,q++);
else if (t==SYN_CH)
curtoken_num=lookahead.tokenVal.number;
lookahead = nextToken( ); lookahead = nextToken( );
} }
else else
...@@ -747,6 +751,15 @@ static EXPVAL Prod_F() ...@@ -747,6 +751,15 @@ static EXPVAL Prod_F()
val.type=p->type; val.type=p->type;
val.val=p->val; val.val=p->val;
} }
else if (lookahead.token==SYN_CH)
{
#if defined(AnaTypeSyn)
printf("SYN: F-->char\n");
#endif
match(SYN_CH);
val.type=ID_CHAR;
val.val.intval=curtoken_num;
}
else if (lookahead.token==SYN_PAREN_L) else if (lookahead.token==SYN_PAREN_L)
{ {
#if defined(AnaTypeSyn) #if defined(AnaTypeSyn)
......
...@@ -43,6 +43,7 @@ static int LexTable[6][8]= ...@@ -43,6 +43,7 @@ static int LexTable[6][8]=
#define ERR -1 #define ERR -1
#define SYN_NUM 1 // int整数 #define SYN_NUM 1 // int整数
#define SYN_ID 2 // id #define SYN_ID 2 // id
#define SYN_CH 3 // 字符
#define SYN_LT 11 // < #define SYN_LT 11 // <
#define SYN_GT 12 // > #define SYN_GT 12 // >
#define SYN_LE 13 // <= #define SYN_LE 13 // <=
...@@ -66,7 +67,7 @@ static int LexTable[6][8]= ...@@ -66,7 +67,7 @@ static int LexTable[6][8]=
#define SYN_TRUE 54 // TRUE #define SYN_TRUE 54 // TRUE
#define SYN_FALSE 55 // FALSE #define SYN_FALSE 55 // FALSE
#define SYN_INT 56 // int #define SYN_INT 56 // int
#define SYN_CHAR 57 // char #define SYN_CHAR 57 // char,此char非彼char,这里是char类型的定义符
#define SYN_IF 58 // if #define SYN_IF 58 // if
#define SYN_ELSE 59 // else #define SYN_ELSE 59 // else
#define SYN_WHILE 60 // while #define SYN_WHILE 60 // while
......
No preview for this file type
main() main()
{ char ch='A'; { char ch;
ch='\\';
show(ch); show(ch);
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment