I am trying to make a Parser with ocamlyacc for a Language, but what type should I put?

134 views Asked by At

I have the following code also have more after like expr: int {} | BOOL {} etc but i dont know what is the type that i should write in type of this parser, i have a calculator example that works with int and the type is int , but in my program i have float char string etc .. Thanks

  %{
dont know what to write here

 %}

 %token <int> INT
 %token <float> FLOAT
 %token <char> CHAR
 %token <bool> BOOL
 %token <string> IDENT
 %token PLUS Div Bigger Smaller MINUS TIMES 
%token TYPE
%token DEF DD
%token Equals Atribuicao SoE BoE And Or
%token IF ELSE BEGIN END WHILE RETURN PV SEQ TO BY OF 
%token RP LP LB RB  
%token EOL
%left Bigger Smaller SoE BoE Equals Atribuicao Or And
%left PLUS MINUS       
%left TIMES Div        
%nonassoc UMINUS OF  

%start main          

%type <> main /* what should be in here ? */


main:
| expr EOL                { $1 }
expr:           
  INT                   {  }    
| BOOL                  {  }  
| FLOAT                 {  }
| CHAR                  {  } 
| expr OF expr              {   }
| BEGIN expr END            {   }
| RETURN expr PV            { $2 }
| LP expr RP                { $2 }
| LB expr RB                { $2 } 
| expr PLUS expr            {  }
| expr MINUS expr           {  }
| expr TIMES expr               {  }
%%

let main() = begin
 Printf.printf "Hello yo\n" ;

end;;
1

There are 1 answers

0
kne On

Judging from your grammar, the return type should be something like expression, because it is expressions that you do parse. How you define that type depends on the semantics you want to implement. I guess that you will need a variant type that can at least hold atoms of type int, bool, float and char. So you can start with

type expression =
| Int of int
| Bool of bool
| Float of float
| Char of char

and see where it takes you.