Gcinbax
Gcinbax

Reputation: 197

Troubles with Flex/Bison

I try set up grammar using flex/bison by next pattern:

DATA: 1,2,3,4,5 PROGRAM: add,mult,div,read

This input must be transfered into

4 add mult div read 5 1 2 3 4 5

Where: 4 is a number of commands after "PROGRAM" and 5 is a number of data after "DATA". At the moment I have next code.

In asm.y (bison):

%{
 #include<stdio.h>

 int comm[100];
 int data[100];
 int ncomm=0;
 int ndata=0;
%}

%token NUMBER

%%

PROGR:"DATA: "INPUT" PROGRAM: "COMMANDS
;

COMMANDS: 
| INSTR","COMMANDS
;

| NUMBER","INPUT {data[ndata]=$1; ndata++;}
;

INSTR:NUMBER {comm[ncomm]=$1; ncomm++;}
|"add" {comm[ncomm]=-10000; ncomm++;}
|"mult" {comm[ncomm]=-10001; ncomm++;}
|"div" {comm[ncomm]=-10002; ncomm++;}
|"minus" {comm[ncomm]=-10003; ncomm++;}
//and so on
;

%%
int main(int argc, char** argv)
{

 yyparse();

 printf("\n%d ",ncomm);
 int i;
 for(i=0; i<ncomm; i++)
 {
  printf("%d ",comm[i]);
 }
 printf("%d ",ndata);
 for(i=0; i<ndata; i++)
 {
  printf("%d ",data[i]);
 }

}

yyerror(char* s)
{
 printf("error: %s\n",s);
}

And in asm.l (flex):

%{
 #include "asm.tab.h"
%}

%%

[0-9]+ {return NUMBER;}

%%

So, after all, program doesn't print right data, but, for example

For

DATA: PROGRAM: add

It prints

DATA: PROGRAM: add

and await for next input.

On the other hand, for

DATA: 5 PROGRAM: add

It prints

DATA: error: syntax error

0 0

And I have no idea what I do wrong.

Upvotes: 0

Views: 109

Answers (1)

akim
akim

Reputation: 8779

You still have a lot of work to do. When you say that it works for DATA: PROGRAM: add, well, it does not: you have been bitten by a "feature" for Lex: unrecognized characters are printed on the standard output.

So:

  • tell Flex you don't want unknown characters to pass-through.

    %option nodefault
    
  • you need to teach Bison that tokens such as "DATA:" will be used. Something like:

    %token ADD "add"
    %token DATA "DATA:"
    

    Then be sure to exactly "DATA:" or DATA in the rest of the file, don't use "DATA: " for instance.

  • you need your scanner/lexer to recognize all the tokens you need: not just numbers, but also keywords (or identifiers) such as add, mult, etc. Something like:

    "+"  return ADD;
    

    but also "DATA:".

    "DATA:" return DATA;
    
  • Make sure to tell your parser generator that a NUMBER has a value:

    %token <int> NUMBER
    
  • and tell you scanner to compute it.

You have much to learn. Before trying to do all this, be sure to try the basic examples from Bison's documentation, and to perfectly understand them.

Upvotes: 1

Related Questions