源文件地址
GitHub - jimmyhuang22/TINY_By_Yacc: 浙江理工大学编译原理实验三、基于YACC的TINY语法分析器的构建
实验要求
运用YACC,针对TINY语言,构造一个语法分析器
实验原理
查找资料可知yacc产生的子程序在申请读入下一个单词时会调用yylex()。yylex()返回一 个单词符号,并将相关的属性值存入全局量yylval
为了联用 lex 和 yacc,需要在运行 yacc 程序时加选项-d,以产生文件 y. tab. h,其中 会包含在 yacc 描述文件中(由% tokens 定义)的所有单词种别。文件 y. tab. h将被包 含在 lex 描述文件中。
输入设计
{
Sample program
in TINY language -
computes factorial
}
read x; { input an integer }
if 0 < x then { don't compute if x <= 0 }
fact := 1;
repeat
fact := fact * x;
x := x - 1
until x = 0;
write fact { output factorial of x }
end
输出设计
TINY COMPILATION: sample.tny
Syntax tree:
Read: x
If
Op: <
Const: 0
Id: x