① 不知道如何生成flex的C程序
這是我以前寫過的一個scanner.l
你試試,應該可以用。
%{
#include <stdio.h>
#include <string.h>
%}
digit [0-9]
number {digit}+
pint ("+")?{number}
mint "-"{number}
realnumber ("+"|"-")?{number}"."?{number}
true "TRUE"
false "FALSE"
letter [a-zA-Z]
identifier {letter}+
newline \n
whitespace [ \t]+
%%
"PROGRAM" {return PROGRAM;}
"BEGIN" {return BEGIN;}
"END" {return END;}
"CONST" {return CONST;}
"INTEGER" {return INTEGER;}
"REAL" {return REAL;}
"BOOLEAN" {return BOOLEAN;}
"ARRAY" {return ARRAY;}
"IF" {return IF;}
"THEN" {return THEN;}
"ELSE" {return ELSE;}
"ENDIF" {return ENDIF;}
"WHILE" {return WHILE;}
"DO" {return DO;}
"REPEAT" {return REPEAT;}
"UNTIL" {return UNTIL;}
"FOR" {return FOR;}
"TO" {return TO;}
"READ" {return READ;}
"WRITE" {return WRITE;}
":=" {strcpy(yylval.cval,yytext);return ASSIGN;}
"=" {strcpy(yylval.cval,yytext);return EQ;}
"<" {strcpy(yylval.cval,yytext);return LT;}
"<=" {strcpy(yylval.cval,yytext);return NMT;}
"<>" {strcpy(yylval.cval,yytext);return NEQ;}
">" {strcpy(yylval.cval,yytext);return MT;}
">=" {strcpy(yylval.cval,yytext);return NLT;}
"+" {strcpy(yylval.cval,yytext);return PLUS;}
"-" {strcpy(yylval.cval,yytext);return MINUS;}
"*" {strcpy(yylval.cval,yytext);return MULTIPLY;}
"/" {strcpy(yylval.cval,yytext);return OVER;}
"AND" {strcpy(yylval.cval,yytext);return AND;}
"OR" {strcpy(yylval.cval,yytext);return OR;}
"NOT" {strcpy(yylval.cval,yytext);return NOT;}
":" {return COLON;}
"#" {return STOP;}
"," {strcpy(yylval.cval,yytext);return COMMA;}
";" {return SEMI;}
"[" {return LSB;}
"]" {return RSB;}
"(" {return LPAREN;}
")" {return RPAREN;}
{pint} {strcpy(yylval.cval,yytext);return PINT;}
{mint} {strcpy(yylval.cval,yytext);return MINT;}
{realnumber} {strcpy(yylval.cval,yytext);return REALNUMBER;}
{true} {strcpy(yylval.cval,yytext);return TRUE;}
{false} {strcpy(yylval.cval,yytext);return FALSE;}
{identifier} {strcpy(yylval.cval,yytext);return ID;}
{newline} {}
{whitespace} {}
. {return ERROR;}
%%
int yywrap()
{
return 1;
}
② Yacc(bison.exe)
將BISON_HAIRY設為F:\LEXYACC\BisonFlex\bison.hairy
BISON_SIMPLE設為F:\LEXYACC\BisonFlex\bison.simple即可