0
votes

I have a Lex and a YACC program to generate a switch parser, which should produce what follows:

switch(var) {
    case 0: z=cost0;
            break;

    case N: z=costN;
            break; 
    default: z=costD;
            break;
  }

and by passing 2 default values (lets say 1 and 2 respectively) which are stored in variables x and y, each one of them would be passed to the val in switch, and all the N conditions would be checked, so, z should be storing variable for the value of the found condition and print it. but i cant write the main function of my YACC to organize the input of x and y , and print z. here is my programs:

Lex:

    %{
    #include"ma1.tab.h"
    %}
    alpha [a-zA-Z]
    digit [0-9]
    %%
    [ \t\n]   
    switch                       return SWITCH;
    int                          return INT;
    case                         return CASE;
    break                        return BREAK;
    default                      return DEFAULT;
    {digit}+                     return NUM;
    {alpha}({alpha}|{digit})*    return ID;
    .                            return yytext[0];
    %%

YACC:

    %{
    #include<stdio.h>
    #include<stdlib.h>
    %}
    %token ID NUM SWITCH CASE DEFAULT BREAK INT
    %right '='
    %%
    program: varassign switchstm {printf("Input accepted.\n");exit(0);}
             ;
    varassign: INT  vardef ‘;’ 
    ;
    vardef : ID '=' NUM | ID '=' NUM varassign {$1=$3;}
    ;
    switchstm:    SWITCH '(' ID ')' '{' block '}'
             ;
    block:    caselist
             |    caselist   defaultstm
            ;
    caselist: casestm | casestm caselist ;

    casestm:   CASE NUM ':' assign ';' BREAK ';'
            ;

    defaultstm :    DEFAULT  ':' assign ';' BREAK ';'
            ;
    assign    : ID'='NUM {$1=$3}
    %%
    main()
    {
    printf("Enter the exp: ");
    yyparse();
    }

but when I run the resulting a.out file, it just does nothing! so my problem is getting around the code in order to get the 2 integers and get them through producing one z per each. Thanks in advance.

1
What have you tried so far? Is the C-language code snippet in your question the code that you want to parse?David Gorsline
Yes. actually I need to generate a parser for this purpose, and I have made the 2 required files of lex and YACC, which I attach below which the first part is the Lexical analyzer and the second is my parser generator(actually I have compiled both, and I have generated the parser right now, but the the YACC's main() isn't complete to take the input x and y and search respectively in the cases of the switch and return z, and I didnt know how to do it!):Reza SA
this is my lex file: %{ #include"ma1.tab.h" %} alpha [a-zA-Z] digit [0-9] %% [ \t\n] switch return SWITCH; int return INT; case return CASE; break return BREAK; default return DEFAULT; {digit}+ return NUM; {alpha}({alpha}|{digit})* return ID; . return yytext[0]; %%Reza SA
Please edit the question to show your code, rather than adding it as a comment.David Gorsline
just did. sorry, im new to this environment.Reza SA

1 Answers

0
votes

So what you have is a parser for a grammar that matches a simple programming language (a switch statment plus a bunch of assignments), but doesn't do anything with it. So it will just run and give you a 'syntax error' if the input doesn't conform to the grammar, or exit silently if it does.

If you want to actually do something with the program (evaluate it? translate it into machine code? bytecode?), you'll need to add actions to the .y file to take the tokens scanned and produce useful data structures and/or output.

Your first step would be dealing with the NUM and ID tokens as these have associated values. You need to define a %union in the yacc code to hold values:

%union {
    int   num;
    char  *id;
}

declare the tokens as using those types:

%token<num>  NUM
%token<id>   ID

and have the lex code return those values:

{digit}+                     { yylval.num = atoi(yytext); return NUM; }
{alpha}({alpha}|{digit})*    { yylval.id = strdup(yytext); return ID; }

with that, you can start adding actions to the yacc code to do things.