前景提示:
个人觉得单纯是用来完成实验报告的话还行,但仅做参考,因为本人的编程水平有限,怕误人子弟。
本次代码支持以下操作:
单行注释
多行注释
文件形式输入
种别码可以在文件中自由修改
单词字符串识别支持:
部分关键字(可手动在程序外部---reference.txt文件添加,),
标识符,
无符号整形数字(仅可识别整形,其他类型需要自主添加别的函数支持),
界符(,{}()[]),
全部运算符(+、=、-、*、/、<、>,以及这些的两两组合)
代码程序:
// 编译原理词法分析器#include #include #include #include #include
运行示例:
文件结构:
示例1:
input.txt文件:
begin begin begin a:=1 end; begin b:=1 end; q:=10 end; x:=9; y:=11; z:=12end#~
reference.txt文件
if $0 then $1 else $2 while $3 begin $4 do $5 end $6 a $7 := $8 + $9 - $10 * $11 / $12 += $13 -= $14 *= $15 /= $16 , $17 ; $22 ( $18 ) $19 { $20 } $21 ident $100 idconst $101
输出
示例2:
input.txt文件:
#include using namespace std;void main(){ cout<<"hello world!!!"< reference.txt文件
if $0 then $1 else $2 while $3 begin $4 do $5 end $6 := $8 + $9 - $10 * $11 / $12 += $13 -= $14 *= $15 /= $16 > $30 < $31 , $17 ; $22 ( $18 ) $19 { $20 } $21 # $1111 include $1112 iostream $1113 using $1114 namespace $1115 std $1116 int $1117 main $1118 cout $1119 endl $1120 void $1121 " $1122 ident $100 idconst $101 ! $1123 << $1124 >> $1125
输出