📄 c-.l
字号:
/* * COMP510-04A COMPILER * Project for 510 * name: Yinghao Qin * * username: yq11 * ID: 0130348 * Date: 16/06/2004 */ %{#include "stdio.h"#include "c-.tab.h"//char yytext[];char *getmem(char *); void count(); //int column = 0;%}eol "\n"linecomment "//"[^\n]*{eol}blockcomment "/*"digit [0-9]letter [A-Za-z_]string \"([^\"])*\"hex [a-fA-F0-9]exponent [Ee][+-]?{digit}+integer {digit}+float {digit}+\.{digit}+identifier {letter}(_|{letter}|{digit})* %%{linecomment} { ++currLine;} {blockcomment} { blcomment();}{string} { count(); yylval.val = getmem(yytext); return (STRING);}"void" { count(); yylval.val = getmem(yytext);return (VOID);}"char" { count(); yylval.val = getmem(yytext);return (CHAR);}"int" { count(); yylval.val = getmem(yytext);return (INT);}"float" { count(); yylval.val = getmem(yytext);return (FLOAT);}"double" { count(); yylval.val = getmem(yytext);return (DOUBLE);}"if" { count(); yylval.val = getmem(yytext);return (IF);}"else" { count(); yylval.val = getmem(yytext);return (ELSE);}"break" { count(); yylval.val = getmem(yytext);return (BREAK);}"continue" { count(); yylval.val = getmem(yytext);return (CONTINUE);}"while" { count(); yylval.val = getmem(yytext);return (WHILE);}"return" { count(); yylval.val = getmem(yytext);return (RETURN);}"goto" { count(); yylval.val = getmem(yytext);return (GOTO);}"do" { count(); yylval.val = getmem(yytext);return (DO);}"switch" { count(); yylval.val = getmem(yytext);return (SWITCH);}"case" { count(); yylval.val = getmem(yytext);return (CASE);}"default" { count(); yylval.val = getmem(yytext);return (DEFAULT);}"for" { count(); yylval.val = getmem(yytext);return (FOR);}"struct" { count(); yylval.val = getmem(yytext);return (STRUCT);}"println" { yylval.val = getmem(yytext);return (PRINTLN);}{integer} { count(); yylval.val = getmem(yytext); return (INT_VAL);}'(\\.|[^\\'])+' { count(); yylval.val = getmem(yytext); return (CHAR_VAL);}0[xX]{hex}+ { count(); yylval.val = getmem(yytext); return (HEX_VAL);}{float} { count(); yylval.val = getmem(yytext); return (FLT_VAL);}{exponent} { count(); yylval.val = getmem(yytext); return (EP_VAL);}"{" { count(); yylval.val = getmem(yytext);return ('{');}"}" { count(); yylval.val = getmem(yytext);return ('}');}"(" { count(); yylval.val = getmem(yytext);return ('(');}")" { count(); yylval.val = getmem(yytext);return (')');}"[" { count(); yylval.val = getmem(yytext);return ('[');}"]" { count(); yylval.val = getmem(yytext);return (']');}"&&" { count(); yylval.val = getmem(yytext);return (AND_OP); }"||" { count(); yylval.val = getmem(yytext);return (OR_OP); }"<=" { count(); yylval.val = getmem(yytext);return (LE_OP); }">=" { count(); yylval.val = getmem(yytext);return (GE_OP); }"==" { count(); yylval.val = getmem(yytext);return (EQ_OP); }"!=" { count(); yylval.val = getmem(yytext);return (NE_OP); }">>=" { count(); yylval.val = getmem(yytext);return (RIGHT_ASSIGN); }"<<=" { count(); yylval.val = getmem(yytext);return (LEFT_ASSIGN); }"+=" { count(); yylval.val = getmem(yytext);return (ADD_ASSIGN); }"-=" { count(); yylval.val = getmem(yytext);return (SUB_ASSIGN); }"*=" { count(); yylval.val = getmem(yytext);return (MUL_ASSIGN); }"/=" { count(); yylval.val = getmem(yytext);return (DIV_ASSIGN); }"%=" { count(); yylval.val = getmem(yytext);return (MOD_ASSIGN); }"&=" { count(); yylval.val = getmem(yytext);return (AND_ASSIGN); }"^=" { count(); yylval.val = getmem(yytext);return (XOR_ASSIGN); }"|=" { count(); yylval.val = getmem(yytext);return (OR_ASSIGN); }">>" { count(); yylval.val = getmem(yytext);return (RIGHT_OP); }"<<" { count(); yylval.val = getmem(yytext);return (LEFT_OP); }"++" { count(); yylval.val = getmem(yytext);return (INC_OP); }"--" { count(); yylval.val = getmem(yytext);return (DEC_OP); }"->" { count(); yylval.val = getmem(yytext);return (PTR_OP); }"~" {count(); yylval.val = getmem(yytext);return (PRN_OP); }"&" {count(); return ('&'); }"!" { count(); return ('!'); }"=" { count(); return ('='); }"." { count(); return ('.'); }":" { count(); return (':'); }";" { count(); return (';'); }"-" {count(); return ('-'); }"+" { count(); return ('+'); }"*" { count(); return ('*'); }"/" {count(); return ('/'); }"%" {count(); return ('%'); }"<" { count(); return ('<'); }">" {count(); return ('>'); }"^" { count(); return ('^'); }"?" {count(); return ('?'); }{identifier} { count(); yylval.val = getmem(yytext); return (ID);}"," { count(); return (',');}\n { count(); ++currLine;}[ \t\v\f]+ {count(); }/* eat up whitespace */. { printf(" Unrecognized character:%s\n", yytext);}%%char *getmem(char *s){char *str;str = (char *)malloc(strlen(s)+1);strcpy(str,s);str[strlen(s)]='\0';return str;}void count(){ int i; for (i = 0; yytext[i] != '\0'; i++) if (yytext[i] == '\n') column = 0; else if (yytext[i] == '\t') column += 8 - (column % 8); else column++;//printf("\n===========%d\n",column); // ECHO;}int blcomment(){int c;while((c=input())!=0){if(c=='\n') ++currLine;if(c=='*'){ // if find * sign, then check the following charactor if((c=input())=='/') // if found / following a *, then comment end break;else unput(c);}}putchar('\n');return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -