zoukankan      html  css  js  c++  java
  • 软件构造——递归下降分析法

    【实验目的】 

    (1)掌握自上而下语法分析的要求与特点。 

    (2)掌握递归下降语法分析的基本原理和方法。 

    (3)掌握相应数据结构的设计方法。  

    【实验内容】 

    用递归下降法编写一个语法分析程序,使之与词法分析器结合,能够根据语言的上下文无关文法,识别输入的单词序列是否文法的句子。

    【实验要求】 

    对下列文法,用递归下降分析法对任意输入的符号串进行分析:

    E->TG    G->+TG|-TG   G->ε

    T->FS    S->*FS|/FS    S->ε

    F->(E)     F->i

    【实验结果】 

     

     代码:

      1 #include <iostream>
      2 #include"yytex.h"
      3 
      4 using namespace std;
      5 char str[10];
      6 int lookahead=0;
      7 void match(char t);
      8 void E();
      9 void T();
     10 void G();
     11 void F();
     12 void S();
     13 void Error();
     14 void Fen();
     15 void Sheng();
     16 int bo = 1;
     17 int Leaf = 1;
     18 int main()
     19 {
     20     cout << "********************************************************" << endl;
     21     cout << "欢迎使用20173599 信1705-1 周博的词法分析器 (采用递归下降分析法)" << endl;
     22     cout << "请输入待检测词法,#号键结束" << endl;
     23     cin >> str;
     24     cout << "文法	分析串	分析字符	剩余串	" << endl;
     25     E();
     26     Error();
     27 }
     28 void Error()
     29 {
     30     
     31     if (bo == 1)
     32     {
     33     
     34         if (str[lookahead] == '#'&&Leaf == 1)
     35         {
     36             cout << "Success" << endl;
     37             
     38         }
     39             
     40         else cout << "ERROR" << endl;
     41     }
     42     bo += 1;
     43 }
     44 void Fen()
     45 {
     46     cout << str[lookahead] << "	";
     47 }
     48 void Sheng()
     49 {
     50     cout << "	";
     51     for (int i = lookahead; i < strlen(str); i++)
     52     {
     53         cout << str[i];
     54     }
     55     cout << endl;
     56 }
     57 void match(char t)
     58 {
     59     Fen();
     60     if (str[lookahead] ==t)
     61     {
     62         cout << t <<"	";
     63         lookahead += 1;
     64         Sheng();
     65     }
     66     else Error();
     67 }
     68 void E()
     69 {
     70     cout << "S->TG		";
     71     Fen();
     72     Sheng();
     73     T();
     74     G();
     75     
     76 }
     77 void T()
     78 {
     79     cout << "T->FS		";
     80     Fen();
     81     Sheng();
     82     F();
     83     S();
     84     Leaf = 0;
     85 }
     86 void G()
     87 {
     88     if (str[lookahead] == '+')
     89     {
     90         cout << "G->+TG	";
     91         match('+');
     92         T();
     93         G();
     94         Leaf = 1;
     95     }
     96     else if (str[lookahead] == '-')
     97     {
     98         cout << "G->-TG	";
     99         match('-');
    100         T();
    101         G();
    102         Leaf = 1;
    103     }
    104     cout << "G->ε		";
    105     Fen();
    106     Sheng();
    107     Leaf = 1;
    108 
    109 }
    110 void F()
    111 {
    112 
    113     if (str[lookahead] == 'i')
    114     {
    115         cout << "F->i	";
    116         match('i');
    117         Leaf = 1;
    118     }
    119 
    120     else if (str[lookahead] == '(')
    121     {
    122         cout << "F->(E)	";
    123         match('(');
    124         E();
    125         if (str[lookahead] == ')')
    126             match(')');
    127         else Error();
    128         Leaf = 0;
    129     }
    130     else
    131     {
    132         Leaf = 0;
    133         Error();
    134         
    135     }
    136 
    137 }
    138 void S()
    139 {
    140     if (str[lookahead] == '*')
    141     {
    142         cout << "S->*FS	";
    143         match('*');
    144         F();
    145         S();
    146     }
    147     else if(str[lookahead] == '/')
    148     {
    149         cout << "S->/FS	";
    150         match('*');
    151         F();
    152         S();
    153     }
    154     cout << "S->ε		";
    155     Fen();
    156     Sheng();
    157     Leaf = 1;
    158 }

    截图:

     

  • 相关阅读:
    C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法
    Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”
    Mysql explain执行计划
    解决Linux c语言运行时候“段错误 (核心已转储)”问题-采用gdb 解决
    udp-->socket通信原理
    udp通信的原理---makefile文件
    c语言知识点
    linux系统man命令用法和安装方法
    <linux系统c语言生成.so文件,生成64位可执行文件,在64位系统中运行32位的可执行文件>
    ubuntu系统无eth0网卡解决办法
  • 原文地址:https://www.cnblogs.com/smartisn/p/12207393.html
Copyright © 2011-2022 走看看