如何来标记中的字符串Lex和Yacc [英] How to tokenize String in Lex and Yacc

查看:269
本文介绍了如何来标记中的字符串Lex和Yacc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参照<一个href=\"https://stackoverflow.com/questions/22346554/reading-new-line-giving-syntax-error-in-lex-yacc\">Reading在LEX YACC 新的生产线给语法错误
 我们使用的lex文件

 %{
        / *
            解析器SSA;
        * /    #包括LT&;&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    的#includey.tab.h
    %}
    %%
    [\\ t] +;
    \\ n;    [IF] +的printf(第一个输入\\ n);
    [其他] +回报(OP);
    [=] +回报(等于);
    [+] +回报(OP);
    [*] +回报(OP);
    [ - ] +回报(OP);    [\\&下;] [B] [B] [] + [1-9] [\\]的计算值{返回(BBLOCK);}    ([_] [A-Z]])|([A-Z] [_] [0-9] +)|([0-9] +){回报(VAR);}    。 ;
    %%

是我应该做的,如果我想获得令牌作为一个字符串即A_2
怎么办呢????

输入文件

  A_2 = 1-6 + B_3;
  a_8 = b_7 - C_5;



解决方案

您可以在您的野牛文件中定义的记号类型:

 %{工会
 字符*串;
}%令牌LT;字符串&GT; VAR

,然后替换

 回归(VAR);

  yylval.string =的malloc(yyleng); sprintf的(yylval.string%s,yytext中);返回VAR;

with reference to Reading new line giving syntax error in LEX YACC lex file we are using

 %{
        /*
            parser for ssa;
        */

    #include<stdio.h>
    #include<stdlib.h>
    #include"y.tab.h"


    %}
    %%
    [\t]+   ;
    \n  ;



    [if]+       printf("first input\n");
    [else]+     return(op);
    [=]+        return(equal);
    [+]+        return(op);
    [*]+        return(op);
    [-]+        return(op);

    [\<][b][b][ ]+[1-9][\>] {return(bblock);}

    ([[_][a-z]])|([a-z][_][0-9]+)|([0-9]+)  {return(var);}

    .   ;




    %%

what should i do if i want to get token as a string i.e a_2 how to do it????

input file is

a_2 = _6 + b_3; 
  a_8 = b_7 - c_5;


解决方案

You can define token type in your bison file:

%union{
 char *string;
}

%token <string> var

and then replace

return(var);

with

yylval.string=malloc(yyleng); sprintf(yylval.string,"%s",yytext);return var;

这篇关于如何来标记中的字符串Lex和Yacc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆