语法分析器的设计与实现程序(c语言) 词法分析器的设计与实现程序(c语言)

qyw7 2002-06-07 04:09:24
赶快
...全文
654 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
joekol 2002-06-10
  • 打赏
  • 举报
回复
D [0-9]
L [a-zA-Z]
H [a-f A-F 0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
%{
#include<stdio.h>
#include"y.tab.h"
void count();
%}
%%
"/*" {comment();}
"auto" {count(); return(AUTO);}
"break" {count(); return(BREAK);}
"case" {count(); return(CASE);}
"char" {count(); return(CHAR);}
"const" {count(); return(CONST);}
"continue" {count(); return(CONTINUE);}
"default" {count(); return(DEFAULT);}
"do" {count(); return(DO);}
"double" {count(); return(DOUBLE);}
"else" {count(); return(ELSE);}
"enum" {count(); return(ENUM);}
"extern" {count(); return(EXTERN);}
"float" {count(); return(FLOAT);}
"for" {count(); return(FOR);}
"goto" {count(); return(GOTO);}
"if" {count(); return(IF);}
"int" {count(); return(INT);}
"long" {count(); return(LONG);}
"reister" {count(); return(REISTER);}
"return" {count(); return(RETURN);}
"short" {count(); return(SHORT);}
"signed" {count(); return(SIGNED);}
"sizerof" {count(); return(SIZEROF);}
"static" {count(); return(STATIC);}
"struct" {count(); return(STRUCT);}
"switch" {count(); return(SWITCH);}
"typedef" {count(); return(TYPEDEF);}
"union" {count(); return(UNION);}
"unsigned" {count(); return(UNSIGNED);}
"void" {count(); return(VOID);}
"volatile" {count();return(VOLATILE);}
"write"{ count();return(write)}
""{l}({L}|{D})*{count();return(check type(1);}
0[xX]{H}{IS}?{count();return(CONSTANT);}
0{D}+{IS}?{count();return(CONSTANT);}
{D}+{IS}? {count();return(CONSTANT);}
L?'(\\.|[^\\']+' {count();return(CONSTANT);}
{D}+{E}{FS}? {count();return(CONSTANT);}
{D]*"."{D}+({E})?{FS}?{count();return(CONSTANT);}
{D]*"."{D}*({E})?{FS}?{count();return(CONSTANT);}
L?'(\\.|[^\\']*' {count();return(STRING-LITERAL);}
"..."{count();return(LLIPSIS);}
">>=" {count();return(RIGHT-ASSIGN);}
"<<="{count();return(LEFT-ASSIGN);}
"+="{count();return(ADD-ASSIGN);}
"-="{count();return(SUB-ASSIGN);}
"*="{count();return(MUL-ASSIGN);}
"/="{count();return(DIV-ASSIGN);}
"%="{count();return(MOD-ASSIGN);}
"&="{count();return(AND-ASSIGN);}
"^="{count();return(XOR-ASSIGN);}
"|="{count();return(OR-ASSIGN);}
">>"{count();return(RIGHT-OP);}
"<<"{count();return(LEFT-OP);}
"++"{count();return(INC-OP);}
"--"{count();return(DEC-OP);}
"->"{count();return(PIR-OP);}
"&&"{count();return(AND-OP);}
"||"{count();return(OR-OP);}
"<="{count();return(LE-OP);}
">="{count();return(GE-OP);}
"=="{count();return(EQ-OP);}
"!="{count();return(NE-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('-');}
"+"{count();return('+');}
"*"{count();return('*');}
"/"{count();return('/');}
"%"{count();return('%');}
"<"{count();return('<');}
">"{count();return('>');}
"^"{count();return('^');}
"|"{count();return('|');}
"?"{count();return('?');}
[\t\v\n\f]{count();}
. {/*ignore bad characters*/}
yywrap()
{return(|);}
comment()
{
char( c1)
loop:
while ((c=input()) !='*' && c!=0)
putchar(c);
if ((c1=input())!='/' && (!=0)
{
unput(c1);
goto loop;
}
if (c!=0)
putchar(c1);
}
int column=0;
void count()
{
int;
for (i=0;yytext[i]!='\0';i++)
if(yytext[i]=='\n')
column=0;
else if(yytext[i]=='\t')
column+=8-(column%8);
else
column++;
ECHO;
}
int check_type()
{
return(IPENTIFIER);}
ritch 2002-06-07
  • 打赏
  • 举报
回复
建议:我用C++做过,C差不多,词法分析就是找关键字,而语法分析我则是用最简单的递归下降法,你去找有关编译原理的书。
ritch 2002-06-07
  • 打赏
  • 举报
回复
建议:我用C++做过,C差不多,词法分析就是找关键字,而语法分析我则是用最简单的递归下降法,你去找有关编译原理的书。
cycker 2002-06-07
  • 打赏
  • 举报
回复
你去找本书《C语言编程实践及实例荟萃》

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧