[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: How to build interperter from a grammar...



The easiest way to deal with that would be to simply create a map of symbols
and values, then when you encounter an assignment production just create an
entry in your map with the variable name and the value, then later when you
refer to it, you can look up the value in your symbol table.

Hope that helps.

-----Original Message-----
From:
sablecc-discussion-bounces+chris.vankirk=fdcjapan.com@xxxxxxxxxxxxxxxxx
[mailto:sablecc-discussion-bounces+chris.vankirk=fdcjapan.com@xxxxxxxxxxxxxx
org] On Behalf Of Roger Pomeroy
Sent: Friday, October 17, 2008 10:03 AM
To: sablecc-discussion@xxxxxxxxxxxxxxxxx
Subject: How to build interperter from a grammar...

I have completed a grammar for my language, but I am somewhat 
confused by what the next step is.  The intent was to build an 
interpreted language, much like the minibasic example.  However, my 
grammar is more complex in that a has productions for more than 
just simple add subtract, etc..., it allows for full parenthesized 
expressions, etc (much like in Java, C, fortran, etc)   I am not 
sure how to go about which routines I need to actually code up in 
order to make the assignment statments work.   I was wondering if 
anyone else perhaps has an example of how to do a more complicated 
interpreted language?  Essentially, I am confused about the fact 
that I have productions that look as shown below.  I realize I need 
routines to store the computed values in a hash table, and be able 
to look up the identifiers and their values from that, but I am not 
sure if I need to do that for each production alternative (ie 
expression, term, expon, factor...) or if I need only do this at 
the lowest (factor level), etc.

If anyone has any example code I could look at, that would help.  I have
looked at both the minibasic and minipascal, but they don't seem to have the
same level of complexity as my grammar...

Thanks!

Roger

  assign_statement      =  lhs  assign_operator  expression  new_line    ;
expression         = {term}   term
                      | {plus}   expression plus        term
                      | {minus}  expression minus       term
                      | {mod}    expression modulo      term
                      | {remain} expression remainder   term
//                     | {condition} conditional_expression
                      ;

   term               = {expon}  expon
                      | {mult}   term star expon
                      | {div}    term slash expon
                      ;

   expon              = {factor} factor
                      | {power}  expon  power factor
                      ;

   factor             = {constant}    unop? constant_expression
                      | {identifier}  unop? identifier 
[additional_identifiers]:additional_identifier*
                      | {array}       unop? array_access
                      | {expression}  l_par expression r_par
                      ;


-- 
Be Yourself @ mail.com!
Choose From 200+ Email Addresses
Get a Free Account at www.mail.com