?? 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 + -