⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sentencecompile.h

📁 一个Pascal子集语言的编译器
💻 H
📖 第 1 页 / 共 4 页
字号:
//SentenceCompile.h




//inputfile:  words.txt,wordstable1.txt
//outputfile: sentences.txt,wordstable2.txt


#ifndef SENTENCECOMPILE_H_
#define SENTENCECOMPILE_H_

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

/*****************************************************************************************
    语法分析器
    
    实现功能:
        1。按照正则表达式,判断语句是否有语法错误。 
        2。如果有错误则需要输入错误类型及行号,没错误则输出一个正确信号。
        3。读取由词法分析器所生成的字符表,并回填相应的类型信息。
        4。要求将过程及函数所用到的变量数量、类型记录下来。 
        5。将程序中的语句分离开,并记录到目标文件以便类型检查使用。 
        6。可以实现的语句类型有以下几种:
         
                                               //statement 
                a ;                            //过程
                a (b) ;                        //带有参数的过程 
                a := b ;                       //赋值 
                if a=b then a:=b else a:=b ;   //条件
                while a=b do a:= b ;           //循环
                read (a,b) ;                   //输入 
                write (a,b) ;                  //输出 
                
    
       回填的信息如下 :
         序号(Index)   类型(Kind)  基本类型(Type)  名称(Name)  参数个数(Have)  所属函数(Belong) 
          
           类型: 
                 程序名         900
                 过程名         800
                 函数名         700
                 变量           100
                 程序参数       990
                 过程参数       810
                 函数参数       710 
                 
                 
           基本类型:
                 整数           101
                 浮点数         102      
                 布尔型         103
                 整数数组       201
                 浮点数数组     202      
                 布尔型数组     203
                 
                 等待输入类型信息       100 
                 等待输入数组类型信息   200 
                 等待输入函数类型信息   700
                 
        举例如下:
            program example(input,output);
            var x,y:integer;
            var abc:real;
            procedure gcd(a,b:integer);
         
            读入信息:
                900 example
                910 input
                910 output
                100 x
                100 y
                800 gcd
                810 a
                810 b
     
            输出信息:
            
               Index   Kind    Type     Name      Have  Belong
                1       900     900     example     2   0
                2       990     101     input       0   101(第一个的第一位) 
                3       990     101     output      0   102
                4       100     101     x           0   1
                5       100     101     y           0   2
                6       700     800     gcd         2   0
                7       710     101     a           0   601                
                8       710     101     b           0   602
                     
                               
    正则表达式:
    
    
     programs   ->  program id ( id_list ) ;                        (1)
                    (declaration) *
                    [ functions ] *
                    [ procedures ] *
                    body .
                    
     declaration->  var id_list : type ;                            (2)
                                                                   
     type       ->  def                                             (3)
                    | 
                    array [ number .. number ] of def               (4)
                    | 
                    record declaration end                          (5)
                    
     functions  ->  function id ( [ id_list : type ] ) : type ;     (6)
                    [ declaration ] *
                    body ;
     
     procedures ->  procedure id ( [ id_list : type ] ) ;           (7)
                    [ declaration ] *
                    body ;
     
                               
     body       ->  begin                                           (8)
                    [ statement ; ] *
                    end
                    |
                    statement
                    
     statement  ->  ids                                             (9)
                    |
                    ids ( simple_list )                             (10)
                    |
                    ids assignop simple                             (11)
                    |
                    while expression do body                        (12)
                    |
                    if expression then body else body               (13)
                    |
                    read ( ids_list )                               (14)
                    |
                    write ( simple_list )                           (15)
                    
     id_list    ->  id , id_list | id                               (16)
     
     ids_list   ->  ids , ids_list | ids                            (17)
                    
     expression ->  simple relop simple                             (18)
     
     simple_list  -> simple | simple , simple_list                  (19)
                    
     simple     ->  factor                                          (20)
                    | 
                    factor op simple                                (21)
                    
     factor     ->  ids                                             (22)
                    |
                    ids ( simple_list )                             (23)
                    |
                    number                                          (24)
                    |
                    not factor                                      (25)
                    |
                    true                                            (26)
                    |
                    false                                           (27)
                    |
                    ( simple )                                      (28)
                    
                         
      其中的非终结符:
         1 programs
         2 declaration
         3 type
         4 functions
         5 procedures
         6 body
         7 statement 
         8 id_list
         9 ids_list
         10 expression
         11 simple_list
         12 simple
         13 factor
      终结符: 
         101 program,102 begin,103 end,104 def,105 function,106 procedure 
         107 if,108 then,109 else,110 while,111 do 
         112 read,113 write,114 array,115 record,116 of
         117 or,118 and,119 not,120 true,121 false 
         122 number,123 id,124 ids 
         125 op,126 relop,127 :,128 ;,129 (,130 ),131 [,132 ],133 .,134 ,  
         135 var
         136 assignop


      写出状态机如下:————————函数:Check(int state,int next)
      
       <凡是下面列出的特殊移进及归约,均在程序块后面加一句 Check(State[StateIndex],next) >
       
       
   1  programs   -> 1000 program 1001 id 1002( 1003 id_list 1004 ) 1005 ; 1006  
                    (declaration) *
                    [ functions ] *
                    [ procedures ] *
                    body 1007 . 9999(结束) 
                    
      1003 ->  123   -> StateIndex++, State[StateIndex]= 8000 
      1006 ->  135   -> StateIndex++, State[StateIndex]= 2000                         
      1006 ->  105   -> StateIndex++, State[StateIndex]= 4000
      1006 ->  106   -> StateIndex++, State[StateIndex]= 5000 
      1006 ->  102   -> StateIndex++, State[StateIndex]= 6000 
      1007 ->  133   -> State = 9999
      9999 ->        -> 输出成功信息
      9999 ->  任意  -> 输出错误信息 
      
      1001->1002: VariantIndex++;
                  Parament=0;
                  VariantType[]=900;
      1005->1006: Parament=0;HaveCount=0;
      1006->6000: Parament=0;HaveCount=0;
      
   2  declaration-> 2000 var 2001 id_list 2002 : 2003 type 2004 ; 2005                         
     
      2001 ->  123   -> StateIndex++, State[StateIndex]= 8000 
      2003 ->  104   -> StateIndex++, State[StateIndex]= 3000
               114
               115 
      2005 ->  任意  -> StateIndex--, Check(State[StateIndex],2) 
      
      2003->3000: TypeNeed=100;
      
      
   3  type       -> 3000 def 3001                                          
                    | 
                    3000 array 3002 [ 3003 number 3004 . 3005 . 3006 number 3007 ] 3008 of 3009 def 3010           
                    | 
                    3000 record 3011 declaration 3012 end 3013    
                                        
      3001 ->  任意  -> StateIndex--, Check(State[StateIndex],3) 
      3010 ->  任意  -> StateIndex--, Check(State[StateIndex],3) 
      3011 ->  135   -> StateIndex++, State[StateIndex]= 2000
      3013 ->  任意  -> StateIndex--, Check(State[StateIndex],3) 
      
      3000->3001: 将Type=100+temp信息回填到VariantType==TypeNeed的VariantType中 
      3009->3010: 将Type=200+temp信息回填到VariantType==TypeNeed的VariantType中 
    * 3000->3011: 将Type=300信息回填到VariantType==TypeNeed的VariantType中
      
   4  functions  -> 4000 function 4001 id 4002 ( 4004 [ id_list 4005 : 4006 type 4007 ] ) 4009 : 4010 type 4011 ; 4012    
                    [ declaration ] *
                    body 4013 ; 4014
      4004 ->  123   -> StateIndex++, State[StateIndex]= 8000
      4006 ->  104   -> StateIndex++, State[StateIndex]= 3000
               114
               115
      4012 ->  135   -> StateIndex++, State[StateIndex]= 2000                         
      4012 ->  102   -> StateIndex++, State[StateIndex]= 6000
      4014 ->  任意  -> StateIndex--, Check(State[StateIndex],4)
      
      4001->4002: VariantIndex++;SubCount++;
                  Temp=VariantIndex;
                  VariantBelong[]=0;
                  Parament=VariantIndex;
                  VariantType=700;等待输入类型 
      4004->8000: HaveCount=0; 
      
      4009->4010: VariantHave=HaveCount;参数个数 
                   
                           
      4006->3000: TypeNeed=100;                                          
      4010->3000: TypeNeed=700; 
      4013->4014: Parament=0;
      
   5  procedures -> 5000 procedure 5001 id 5002 ( 5003 [ id_list 5004 : 5005 type 5006 ] ) 5007 ; 5008         
                    [ declaration ] *
                    body 5009 ; 5010
                    
      5003 ->  123   -> StateIndex++, State[StateIndex]= 8000 
      5005 ->  104 114 115  -> StateIndex++, State[StateIndex]= 3000
      5008 ->  135   -> StateIndex++, State[StateIndex]= 2000                         
      5008 ->  102   -> StateIndex++, State[StateIndex]= 6000
      5010 ->  任意  -> StateIndex--, Check(State[StateIndex],5)
      
      5001->5002: VariantIndex++;SubCount++;
                  Temp=VariantIndex;
                  VariantBelong[]=0;
                  Parament=VariantIndex;
                  VariantType=800;
      5003->8000: HaveCount=0;
      5005->3000: TypeNeed=100;
      5007->5008: VariantHave=HaveCount;            
                  
   6  body       -> 6000 begin 6001                                     
                    [ [ statement 6002 ; 6003 ] statement 6002 ] 
                    end 6004
                    |
                    6000 statement 6005
                    
      6001 -> 124    -> StateIndex++, State[StateIndex]= 7000 
              107
              110
              112
              113              
      6000 -> 124    -> StateIndex++, State[StateIndex]= 7000 
              107
              110
              112
              113              
      6004 ->  任意  -> StateIndex--, Check(State[StateIndex],6)
      6005 ->  任意  -> StateIndex--, Check(State[StateIndex],6)
      
   7  statement  -> 7000 ids 7001                                          
                    |
                    7000 ids 7001 ( 7002 simple_list 7003 ) 7004                           
                    |
                    7000 ids 7001 assignop 7005 simple 7006                          
                    |
                    7000 while 7007 expression 7008 do 7009 body 7010                      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -