📄 wordcompile.h
字号:
//WordCompile.h
//inputfile: 所给定的源文件
//outputfile: words.txt,wordtable1.txt
//tempfile: temp.txt
#ifndef WORDCOMPILE_H_
#define WORDCOMPILE_H_
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
/*****************************************************************************************
词法分析器
作用:从所给的文件中依次读取出字符,进行词法分析,判断所使用的词是否拼写正确,并建立起变量表。
要求:1。将每个词和符号都使用空格分开以便以后的语法分析
2。去除所有的注释
3。将定义的变量及其类型写入到符号表中
4。查找出所有变量定义不合法的,或是重复定义的变量
5。给出错误信息
6。输出文件与输入文件的每行要互相对应。
分工:此部分分成两大部分:变量扫描,关键字扫描。
变量扫描要求:
1。在定义过程中所定义到的标识符,需要用符号表记录下来。并根据类型及名称判断是否声明重复。
2。对于一般过程中所使用到的标识符,需要与符号表进行对照,如果匹配则通过检测。
3。对于没有通过检测的变量要给出错误信息
4。注释部分要在此模块中被去除掉
变量表:包括变量类型及名称,类型定义如下
WORDTABLE
类型 标识号
变量 id
整数integer 101
浮点数real 102
布尔值boolean 103
整数数组 211
浮点数数组 212
布尔值数组 213
过程Procedure 800
过程的参数 811
812
813
返回整数的函数 701
返回浮点数的函数 702
返回布尔值的函数 703
函数参数 711
712
713
程序名 900
程序参数 910
关键字扫描要求:
1。能查询出各个关键字,并按照特定格式输出,如"+"输出为op 1。
2。找到定义变量的关键字,并给出允许输入变量标识符的特定状态
此类关键字包括program , procedure , function , var
3。关键字的编码:作出以下编码表:
关键字 标识号
@program program
@begin begin
@end end
@integer def 1
@real def 2
@boolean def 3
@function fundction
@procedure procedure
@var var
@if if
@then then
@else else
@while while
@do do
@read read
@write write
@array array
@of of
@record record
@or or
@and and
@not not
@true true
@false false
number(数字) number
: :
:= =
; ;
' '
" "
( (
) )
[ [
] ]
+ op 1
- op 2
* op 3
/ op 4
div op 5
mod op 6
= relop 0
> relop 1
< relop 2
<= relop 3
>= relop 4
<> relop 5
. .
, ,
(建议在文件尾部添加一个0,以便语法分析分析)
状态机的建立:
(均有Line++)
a b c d e f g h i j k l m n o p q r s t u v w x y z 0-9 : = ( ) [ ] { } + - * / > < . , 空格 回车
2000 9999
9999 2000
(program,procedure)
2000 2001 ---------------------对照符号表---------------------
2001 -------其他均转至5000---- 2002
2002 2003
2003 2007 2004
2004 2005
2005 2006
2006 8881
2007 2008
2008 2009
2009 2010
2010 2011
2011 8882
(begin,boolean)
2000 2012
2012 2013 2016 ----------------对照符号表-------------
2013 2014
2014 2015
2015 8888
2016 2017
2017 2018
2018 2019
2019 2020
2020 8803
a b c d e f g h i j k l m n o p q r s t u v w x y z 0-9 : = ( ) [ ] { } + - * / > < . , 空格 回车
(end)
2000 2021
2021 2022
2022 8888
(integer)
2000 2023
2023 2024
2024 2025
2025 2026
2026 2027 ----------------对照符号表-------------
2027 2028
2028 8801
(real)
2000 2029
2029 2030
2030 2031
2031 8802
(function)
2000 2032
2032 2033
2033 2034
2034 2035
2035 2036
2036 2037
2037 2038
2038 8888
a b c d e f g h i j k l m n o p q r s t u v w x y z 0-9 : = ( ) [ ] { } + - * / > < . , 空格 回车
(if)
2000 2023
2023 8888
(then)
2000 2040
2040 2041
2041 2042
2042 8888 ----------------对照符号表----------------
(else)
2000 2021
2021 2043
2043 2044
2044 8888
(while)
2000 2045
2045 2046
2046 2047
2047 2048
2048 8888
(do)
2000 2049
2049 8888
(read)
2000 2029
2029 2030
2030 2031
2031 8888
(write)
2000 2045
2045 2050
2050 2051
2051 2152
2152 8888
a b c d e f g h i j k l m n o p q r s t u v w x y z 0-9 : = ( ) [ ] { } + - * / > < . , 空格 回车
(array)
2000 2052
2052 2053
2053 2054
2054 2055
2055 8888
(of)
2000 2056
2056 8888
(record)
2000 2029 ----------------对照符号表----------------
2029 2030
2030 2057
2057 2058
2058 2059
2059 8888
a b c d e f g h i j k l m n o p q r s t u v w x y z 0-9 : = ( ) [ ] { } + - * / > < . , 空格 回车
(div)
2000 2049
2049 2060
2060 8888
(mod)
2000 2061
2061 2062
2062 8888
(and)
2000 2052
2052 2063
2063 8888
(or)
2000 2056
2056 8888
(var)
2000 2064
2064 2065
2065 8800
(not)
2000 2066
2066 2067
2067 8888
(true)
2000 2040
2040 2068
2068 2069
2069 8888
(false)
2000 2032
2032 2070
2070 2071
2071 2072
2072 8888
a b c d e ....y z 0-9 : = ; ' " ( ) [ ] { } + - * / > < . , 空格 回车
(numbers数字)
2000 3000
3000 -------错误------ 3000 ---------------2000_输出" number"-------------------
3000 3001
3001 -------错误------ 3001 ---------------2000_输出" number"-------------------
(:)
2000 4001
4001 ----------------------(除"="外)2000_输出" :"----------------------------------
(:=)
4001 2000_输出" ="
(;) 输出" ;"
(') 输出" '"
(") 输出" ""
(() 输出" ("
()) 输出" )"
([) 输出" ["
(]) 输出" ]"
(+) 输出op 1
(-) 输出op 2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -