Untitled
user_2381398
plain_text
a year ago
3.7 kB
1
Indexable
Never
%{ #define LIST strcat(buf,yytext) #define token(t) {LIST; printf("<%s>\n",#t);} #define tokenInteger(t,i) {LIST; printf("<%s:%d>\n",t,i);} #define tokenString(t,s) {LIST; printf("<%s:%s>\n",t,s);} #define T_STRING_LITERAL "stringLiteral" using namespace std; #include <iostream> #include <vector> #include <map> #define MAX_LINE_LENG 256 int linenum = 1; char buf[MAX_LINE_LENG]; char strbuf[MAX_LINE_LENG]; bool Opt_L = true; bool stringflag = false; class SymbolTable{ public: SymbolTable(); int dump(); int lookup(string s); int insert(string s); private: vector<string> Syms; map<string,int> SymMap; int index; }; SymbolTable *ST; %} dig [0-9] integer {dig}+ identifier [a-zA-Z]({dig}|[a-zA-Z])* numericalConstants ({dig}+\.{dig}*) stringConstants (\"(\"\"|[^"\n])*\") space [ \t]+ %x COMMENTS %x STRING %% "." {token('.');} "," {token(',');} ":" {token(':');} ";" {token(';');} "(" {token('(');} ")" {token(')');} "[" {token('[');} "]" {token(']');} "{" {token('{');} "}" {token('}');} "+" {token('+');} "-" {token('-');} "*" {token('*');} "/" {token('/');} "mod" {token('mod');} ":=" {token(':=');} "<" {token('<');} "<=" {token('<=');} ">" {token('>');} ">=" {token('>=');} "==" {token('==');} "=" {token('=');} "not=" {token('not=');} "and" {token('and');} "or" {token('or');} "not" {token('not');} "array" {token(ARRAY);} "begin" {token(BEGIN);} "bool" {token(BOOL);} "char" {token(CHAR);} "const" {token(CONST);} "decreasing" {token(DECREASING);} "default" {token(DEFAULT);} "do" {token(DO);} "else" {token(ELSE);} "end" {token(END);} "exit" {token(EXIT);} "false" {token(FALSE);} "for" {token(FOR);} "function" {token(FUNCTION);} "get" {token(GET);} "if" {token(IF);} "int" {token(INT);} "loop" {token(LOOP);} "of" {token(OF);} "put" {token(PUT);} "procedure" {token(PROCEDURE);} "real" {token(REAL);} "result" {token(RESULT);} "return" {token(RETURN);} "skip" {token(SKIP);} "string" {token(STRING);} "then" {token(THEN);} "true" {token(TRUE);} "var" {token(VAR);} "when" {token(WHEN);} {identifier} { tokenString("identifier",yytext); ST->insert(yytext); } {integer} { tokenInteger("integer",atoi(yytext)); } {numericalConstants} { tokenString("numericalConstants", yytext); } "%"[^\n]* {LIST;} "{%" { LIST; BEGIN COMMENTS; } <COMMENTS>\n { LIST; if(Opt_L) printf("%d: %s", linenum, buf); linenum++; buf[0] = '\0'; } <COMMENTS>. { LIST; } <COMMENTS>"%}" { LIST; BEGIN INITIAL; } "\"" { LIST; strbuf[0] = '\0'; BEGIN STRING; } <STRING>\" { tokenString(T_STRING_LITERAL, strbuf); strbuf[0] = '\0'; } <STRING>\"\" { LIST; strcat(strbuf, "\""); } <STRING>. { LIST; strcat(strbuf,"\""); } \n { LIST; if(Opt_L) printf("%d: %s", linenum++, buf); buf[0] = '\0'; } {space} {LIST;} . { LIST; if(Opt_L) { printf("%d:%s\n", linenum+1, buf); printf("error input:'%s'\n",yytext); } exit(-1); } %% SymbolTable::SymbolTable(){ index = 0; } int SymbolTable::lookup(string s){ if(SymMap.find(s) != SymMap.end()){ return SymMap[s]; }else{ return -1; } } int SymbolTable::insert(string s){ if(SymMap.find(s) != SymMap.end()){ return -1; } else{ Syms.push_back(s); SymMap[s] = index; index++; return index-1; } } int SymbolTable::dump(){ for(int i=0;i<index;i++){ printf("%d : %s\n",i,Syms[i].c_str()); } return Syms.size(); } void create(){ ST = new SymbolTable(); } int main(){ create(); yylex(); cout << "\n\nSymbol Table: \n"; ST->dump(); }