BISON - 如何输入完整的输入?

时间:2016-01-27 06:45:48

标签: bison yacc lex

如何输入完整语句,例如x = 2而不是像:

input: x *enter*
input: = *enter*
input: 2 *enter* 

目前我设法在BISON上输入输入但是分开而不是只输入一个声明。我从单个输入获得输出但是当我尝试在一个语句中输入所有输出时没有输出。

y file:

 %{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void yyerror(char *s);
int yylex(void);
int yydebug = 1;

%}




%union { char * stringValue;
        int integer;
        double real;
        char * string;

        char expresion;
 }

%start program

%token ASSIGNMENT
%token IF
%token THEN
%token END
%token PRINT
%token READ
%token REM
%token LOGICAL
%token ARITHMETIC
%token COMPARISON
%token ERRORTOKEN

%token <integer> INTNUMBER
%token <real> REALNUMBER
%token <stringValue> STRING

%token <string> INTTOKEN REALTOKEN STRINGTOKEN



%%

program:        /* empty */
                |
                program exp {;}
                ;

line:           stmt
                ;
exp:          INTTOKEN {printf("Token");}
            | INTNUMBER {printf("Number");}
            | ASSIGNMENT      {printf("Sign");}
            | INTTOKEN ASSIGNMENT INTNUMBER {printf("Assignment");}
            ;
stmt:       exp
            ;

%%

void yyerror (char *s) {fprintf (stderr, "%s\n", s);}


int main (void) {

  yydebug = 1;
  return yyparse();

}

Lex档案:

    %{
#include "y.tab.h"
#include <stdio.h>
#include <string.h>

%}

%%


=                                   return ASSIGNMENT;
if|IF                       return IF;
then|THEN               return THEN;
end\.|END\.             return END;
print|PRINT             return PRINT;
read|READ                   return READ;
rem|REM                     return REM;
\.gt\.                      return COMPARISON;
\.eq\.                      return COMPARISON;
\.lt\.                      return COMPARISON;
\.ge\.                      return COMPARISON;
\.le\.                      return COMPARISON;
\.ne\.                      return COMPARISON;
\.and\.                   return LOGICAL;
\.or\.                      return LOGICAL;
\.not\.                     return LOGICAL;
\.add\.                     return ARITHMETIC;
\.mul\.                     return ARITHMETIC;
\.div\.                     return ARITHMETIC;
\.sub\.                     return ARITHMETIC;
\".*\"                      { yylval.stringValue = yytext; return STRING; }
[0-9]+                      { yylval.integer = atoi(yytext); return INTNUMBER; }
[0-9]+\.[0-9]+          { yylval.real = atoi(yytext); return REALNUMBER;}
[a-fA-F][a-zA-Z]*       { yylval.string = yytext; return INTTOKEN; }
[g-nG-N]+[a-zA-Z]*      { yylval.string = yytext; return REALTOKEN; }
[o-zO-Z]+[a-zA-Z]*      {   yylval.string = yytext; return STRINGTOKEN; }
.*                                  ;
%%

int yywrap(void) {
    return -1;
}

1 个答案:

答案 0 :(得分:0)

.*更改为.并修复。我的部分真的很乱。