!!!TINYCPU用コンパイラの字句解析 入力のC言語風プログラムからトークンを切り出す. !!トークンリスト ,トークン,文字列 ,AND,&& ,OR,|| ,EQ,== ,NE,!= ,GE,>= ,LE,<= ,SHL,<< ,SHR,>> ,goto,GOTO ,IF,if ,ELSE,else ,WHILE,while ,DO,do ,OUT,out ,INT,int ,IN,in ,HALT,halt ,NUMBER,[0-9]+ (数字の列) ,NAME,[a-zA-Z][a-zA-Z0-9]* (英字で始まる英数字の列) !!ソースコード %{ #include #include "y.tab.h" int n=0; %} %% [ \t\n\r] && {return(AND);} \|\| {return(OR);} == {return(EQ);} != {return(NE);} \>= {return(GE);} \<= {return(LE);} \<\< {return(SHL);} \>\> {return(SHR);} do {yylval.n=++n;return(DO);} else {return(ELSE);} goto {return(GOTO);} halt {return(HALT);} if {yylval.n=++n;return(IF);} in {return(IN);} int {return(INT);} out {return(OUT);} while {yylval.n=++n;return(WHILE);} [0-9]+ {yylval.s=strdup(yytext);return(NUMBER);} [a-zA-Z][a-zA-Z0-9]* {yylval.s=strdup(yytext);return(NAME);} . {return(yytext[0]);} %% int yywrap(){ return(1);}