Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Compiler construction 我需要帮助在XML文件中构造AST(抽象语法树)_Compiler Construction_Bison_Abstract Syntax Tree_Flex Lexer - Fatal编程技术网

Compiler construction 我需要帮助在XML文件中构造AST(抽象语法树)

Compiler construction 我需要帮助在XML文件中构造AST(抽象语法树),compiler-construction,bison,abstract-syntax-tree,flex-lexer,Compiler Construction,Bison,Abstract Syntax Tree,Flex Lexer,我不知道如何解决这个家庭作业的问题;我们被告知要为一种名为Exp的语言编写一个flex和bison代码,该语言提供结构控制:序列、条件和重复。 我编写了代码,但不知道如何构造AST并将其显示在XML文件中。以下是Flex代码: START%{ #include"parser.tab.h" %} lettre [a-z] chiffre [0-9] %x comment %% "/*"

我不知道如何解决这个家庭作业的问题;我们被告知要为一种名为Exp的语言编写一个flex和bison代码,该语言提供结构控制:序列、条件和重复。 我编写了代码,但不知道如何构造AST并将其显示在XML文件中。以下是Flex代码:

START%{
        #include"parser.tab.h"

    %}

    lettre  [a-z]
    chiffre [0-9]

    %x comment
    %%


    "/*"                               BEGIN(comment);
    <comment>.                         ;
    <comment>"*/"                      BEGIN(INITIAL);

    debut                               return DEBUT;
    fin                                 return END;
    si                                  return IF;
    alors                               return ALORS;
    sinon                               return SINON;
    repeter                             return REPETER;
    jusqua                              return JUSQUA;
    pour                                return POUR;
    tantque                             return TANTQUE;
    lire                                return READ;
    ecrire                              return ECRIRE;

    " "|\t                             ;


    {lettre}({chiffre}|{lettre}){0,35}  {return ID;}
    "-"?{chiffre}+                      {return NUM;}
    "=="                                return EQUAL;

    "*"|"/"|"+"|"-"|"="|"<"|">"         return *yytext;
    "("|")"|","|";"|"."                 return *yytext;

    \n                                  ;

    .                                   {
                                        printf("erreur\n");
                                        return 0;
                                        }
    %%
START%{
#包括“parser.tab.h”
%}
lettre[a-z]
奇弗尔[0-9]
%x评论
%%
“/*”开始(注释);
.                         ;
“*/”开始(首字母);
首秀-回归首秀;
翅片回流端;
si返回IF;
孤注一掷;
西农返回西农;
返波器;
jusqua返回jusqua;
倒灌回灌;
Tantquee返回Tantquee;
lire返回读取;
ecrire返回ecrire;
“”|\t;
{lettre}({chiffre}{lettre}){0,35}{return ID;}
“-”?{chiffre}+{return NUM;}
“=”返回相等;
*“|”/“|”+“|”-“|”=“|”返回*文本;
“(“|”)返回*文本;
\n;
.                                   {
printf(“erreur\n”);
返回0;
}
%%
这是野牛密码

 %{
        #include<stdio.h>


        extern FILE* yyin;

        int numAffec = 0;
        int numLecture = 0;
        int numEcriture = 0;
        int numnumCondition = 0;
        int numPour = 0;
        int numTantque = 0;
        int numRepeter = 0;
    %}


    %start prog
    %token DEBUT END IF ALORS SINON REPETER JUSQUA POUR TANTQUE READ ECRIRE NUM ID EQUAL
    %%

    prog : DEBUT seq_instr END '.'

    seq_instr : seq_instr instr ';'
            | instr ';'

    instr : instr_si           {numCondition++;}
            | instr_repeter    {numRepeter++;}
            | instr_pour       {numPour++;}
            | instr_tant_que   {numTantque++;}
            | instr_aff        {numAffec++;} 
            | instr_lect       {numLecture++;} 
            | instr_ecrit

    instr_si : IF exp ALORS seq_instr END 
            |  IF exp ALORS seq_instr SINON seq_instr END 
            ;


    instr_repeter : REPETER seq_instr JUSQUA '(' exp ')' 
                  ;               
    instr_pour : POUR '(' instr_aff ',' exp ',' instr_aff ')' seq_instr END 
                  ;  
    instr_tant_que : TANTQUE '(' exp ')' seq_instr END 
                   ; 
    instr_aff : ID '=' exp  
              ;  
    instr_lect : READ '(' ID ')'   
               ; 
    instr_ecrit : ECRIRE '(' exp ')'    
                ;
    exp : exp_simple '<' exp_simple
        | exp_simple '>' exp_simple
        | exp_simple EQUAL exp_simple
        | exp_simple

    exp_simple : exp_simple '+' term
                | exp_simple '-' term
                | term

    term : term '*' facteur
         | term '/' facteur
         | facteur

    facteur : '(' exp ')'
            | NUM
            | ID

    %%
    int main(int arg,char** var) {

        yyin = fopen(var[1],"r");

        if(yyin == NULL) {
            printf("erreur\n");
            return ;
        }

        yyparse();

        printf("Affectation    : %d\n",numAffec);
        printf("Lecture        : %d\n",numLecture);
        printf("Ecriture       : %d\n",numEcriture);
        printf("Conditionnel   : %d\n",numCondition);
        printf(" Pour    : %d\n",numPour);
        printf(" Tant Que: %d\n",numTantque);
        printf(" Repeter : %d\n",numRepeter);

        return 0;
    }
%{
#包括
外部文件*yyin;
int numafec=0;
整数结构=0;
int numeriture=0;
int numcondition=0;
int numPour=0;
int numTantque=0;
int numRepeter=0;
%}
%启动程序
%令牌首次登场结束,如果ALORS SINON REPETER JUSQUA POUR tantquee读取的ECRIRE NUM ID相等
%%
程序:首次登场顺序仪表结束“
顺序仪表:顺序仪表';'
|仪表“;”
instr:instr_si{numCondition++}
|仪器{numRepeter++;}
|instr_pour{numPour++;}
|仪器{numTantque++}
|instr_aff{numAffec++;}
|指令选择{numLecture++;}
|仪表盘
仪表si:如果exp ALORS seq_instr END
|如果exp ALORS seq\u instr SINON seq\u instr END
;
仪器重复:重复顺序为“仪器重复”
;               
仪表浇注:浇注“(“仪表装配”、“试验”、“仪表装配”)”后接仪表端
;  
仪器:仪器结束后的“测试”
; 
仪表安装:ID'='exp
;  
指令选择:读“('ID')”
; 
仪器名称:ECRIRE'('exp')'
;
exp:exp\u simple“”exp\u simple
|exp\u simple等于exp\u simple
|简单的
exp_simple:exp_simple'+'术语
|exp_简单的“-”术语
|术语
术语:术语“*”事实陈述人
|术语“/”事实陈述人
|制造者
事实陈述人:“(“经验”)”
|NUM
|身份证
%%
int main(int参数,字符**var){
yyin=fopen(var[1],“r”);
如果(yyin==NULL){
printf(“erreur\n”);
返回;
}
yyparse();
printf(“假装:%d\n”,numafec);
printf(“讲座:%d\n”,数字结构);
printf(“Ecriture:%d\n”,numeriture);
printf(“条件名称:%d\n”,numCondition);
printf(“浇注:%d\n”,numPour);
printf(“tantque:%d\n”,numTantque);
printf(“重复:%d\n”,numRepeter);
返回0;
}
有关构建AST的一般方法,请参见

你需要为野牛量身定做这个;事实上,人们一直在这样做,IIRC,《野牛参考手册》给出了如何做到这一点的例子。所以,你需要仔细阅读野牛参考手册


一旦有了AST,就可以通过执行递归树遍历并在遍历时吐出XML标记/内容来从中生成XML。这是一段非常琐碎的代码。

你被否决了,因为你没有证据表明你试图解决这个问题。你试了什么?你读了什么,你不懂?