parsing - Transform grammar into LL(1) -


i have following grammar:

start -> stm $ stm -> var = expr stm -> expr expr -> var var -> id var -> * expr 

with firstand follow sets:

          first set     follow set start       id, *       $ stm         id, *       $ expr        id, *       $, = var         id, *       $, = 

i've created parsing table follows:

                $           =           id              *               $ start                           start → stm $        start → stm $ stm                             stm → var = expr    stm → var = expr                                 stm → expr          stm → expr expr                            expr → var          expr → var var                             var → id            var → id                                 var → * expr        var → * expr 

from here can see not ll(1).

how can modify grammar becomes ll(1)?

if think sorts of strings can generated particular grammar, it's strings of 1 of following forms:

 ***....**id  ***....**id = ***...**id 

with in mind, can design ll(1) grammar language building new grammar language scratch. here's 1 way this:

start → statement $

statement → starredid optexpr

starredid → * starredid | id

optexpr → ε | = starredid

here, first sets given follows:

  • first(start) = {*, id}
  • first(statement) = {*, id}
  • first(starredid) = {*, id}
  • first(optexpr) = {ε, *, id}

  • follow(statement) = {$}

  • follow(starredid) = {=, $}
  • follow(optexpr) = {$}

the parse table shown here:

                  *                 | id                | =             $  ---------------+-------------------+-------------------+-------------+-----------  start          | statement$        | statement$        |             |  statement      | starredid optexpr | starredid optexpr |             |  starredid      | * starredid       | id                |             |  optexpr        |                   |                   | = starredid | epsilon 

so grammar ll(1).


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -