实验二直接上代码吧,其实我也不怎么知道哪里是致命的,课上听老师的课后,将代码写上,但是运行在环境里面还是不行,把注释去掉就可以了,不知道会不会是注释的问题
%{
#include <stdio.h>
#include "define.h"
int lineno = 1;
TokenType id2keyword(const char* token);
%}
newline \n
whitespace [ \t]+
id [a-zA-Z_]+[a-zA-Z0-9_]*
num ([1-9]+[0-9]*)|([1-9])|0
%%
":=" { return ASSIGN; }
"=" { return EQ;}
"<" { return LT; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return OVER; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMI; }
{id} {
return id2keyword(yytext);
}
{num} { return NUM; }
{newline} { lineno++; }
{whitespace} { }
"{" {
char c;
do
{
c = input();
if(c == EOF)
{
return ERROR;
}
else if(c == '\n')
lineno++;
else if(c == '}')
break;
}while(1);
}
. { return ERROR; }
%%
void printToken(TokenType token,char* tokenString);
int main(int argc, char* argv[])
{
TokenType tt;
if(argc < 2)
{
printf("Usage: scan.exe filename.\n");
return 1;
}
FILE* file = fopen(argv[1], "rt");
if(NULL == file)
{
printf("Can not open file \"%s\".\n", argv[1]);
return 1;
}
yyin = file;
while((tt = yylex()) != 0)
{
printToken(tt, yytext);
if(tt==ERROR)
break;
}
fclose(file);
return 0;
}
typedef struct _KeyWord_Entry
{
const char* word;
TokenType type;
}KeyWord_Entry;
static const KeyWord_Entry key_table[] =
{
{ "if", IF },
{ "then", THEN },
{ "else", ELSE },
{ "end", END },
{ "repeat", REPEAT },
{ "until", UNTIL },
{ "read", READ },
{ "write", WRITE }
};
TokenType id2keyword(const char* id)
{
int i;
for (i = 0; i < 8; i++)
{
if (!strcmp(id, key_table[i].word))
return key_table[i].type;
}
return ID;
}
void printToken(TokenType token,char* tokenString)
{
switch (token)
{
case IF:
printf("line%2d: %-6d%-10s%s\n", lineno,IF,"IF", "if");
break;
case THEN:
printf("line%2d: %-6d%-10s%s\n", lineno,THEN, "THEN","then");
break;
case ELSE:
printf("line%2d: %-6d%-10s%s\n",lineno, ELSE, "ELSE" "else");
break;
case END:
printf("line%2d: %-6d%-10s%s\n", lineno,END,"END", "end");
break;
case REPEAT:
printf("line%2d: %-6d%-10s%s\n", lineno,REPEAT,"REPEAT", "repeat");
break;
case UNTIL:
printf("line%2d: %-6d%-10s%s\n", lineno,UNTIL,"UNTIL", "until");
break;
case READ:
printf("line%2d: %-6d%-10s%s\n",lineno, READ,"READ", "read");
break;
case WRITE:
printf("line%2d: %-6d%-10s%s\n",lineno, WRITE, "WRITE", "write");
break;
case ASSIGN:
printf("line%2d: %-6d%-10s%s\n",lineno, ASSIGN,"ASSIGN", ":=");
break;
case LT:
printf("line%2d: %-6d%-10s%s\n",lineno, LT,"LT", "<");
break;
case EQ:
printf("line%2d: %-6d%-10s%s\n", lineno,EQ, "EQ","=");
break;
case LPAREN:
printf("line%2d: %-6d%-10s%s\n", lineno,LPAREN," LPAREN" ,"(");
break;
case RPAREN:
printf("line%2d: %-6d%-10s%s\n", lineno,RPAREN,"RPAREN", ")");
break;
case SEMI:
printf("line%2d: %-6d%-10s%s\n", lineno,SEMI,"SEMI", ";");
break;
case PLUS:
printf("line%2d: %-6d%-10s%s\n", lineno,PLUS, "PLUS","+");
break;
case MINUS:
printf("line%2d: %-6d%-10s%s\n", lineno,MINUS, "MINUS","-");
break;
case TIMES:
printf("line%2d: %-6d%-10s%s\n", lineno,TIMES, "TIMES","*");
break;
case OVER:
printf("line%2d: %-6d%-10s%s\n", lineno,OVER,"OVER", "/");
break;
case ENDFILE:
printf("line%2d: %-6d%-10s%s\n", lineno,ENDFILE,"ENDFILE", "endfile");
break;
case NUM:
printf("line%2d: %-6d%-10s%s\n", lineno,NUM,"NUM", tokenString);
break;
case ID:
printf("line%2d: %-6d%-10s%s\n", lineno,ID, "ID",tokenString);
break;
case ERROR:
printf("line%2d: %-6d%-10s\n", lineno,ERROR, "ERROR");
break;
default: return ;
}
}
标签:return,分析器,zA,词法,lex,include
From: https://www.cnblogs.com/hellciw/p/17488438.html