A question related to formal language theory and parsing in compiler design

34 views Asked by At

I am trying to extend the grammar of the Tiny language. "reg" represents regular expressions, "&" signifies concatenation, "#" denotes closure, "?" indicates an optional element, and "|" represents choice. Parentheses are supported as well. "bitwise" is used for bitwise expressions. Here is the grammar I have compiled.

assign-stmt->identifier := exp | identifier := reg | identifier += exp
exp->mix-exp or bitwiseor | bitwiseor
bitwiseor->bitwiseor and cmp-exp | cmp-exp
cmp-exp->simple-exp comparison-op simple-exp | simple-exp
comparison-op-> < | = | > | <= | >= | <>
simple-exp->simple-exp addop term | term
addop-> + | -
term->term mulop power | power
mulop-> * | / | %
power->bitwisenot^power | bitwisenot
bitwisenot->not bitwisenot | factor
factor->(exp) | number | identifier

reg->reg "|" regor | regor
regor->regor "&" regand | regand
regand->regcl"#" | regcl"?" | regcl
regcl->(reg) | identifier

When I simplified the grammar, I found that within the assign-stmt, it's not possible to distinguish between exp and reg.In the recursive descent program, there is an issue within the assign function where it cannot determine whether to call the exp function or the reg function. Is there a problem with this?

assign-stmt->identifier (:= exp | := reg | += exp)                                                                        
exp->bitwiseor {or bitwiseor}
bitwiseor->cmp-exp {and cmp-exp}
cmp-exp->simple-exp [comparison-op simple-exp]
comparison-op-> < | = | > | <= | >= | <>
simple-exp->term {addop term}
addop-> + | -
term->power {mulop power}
mulop-> * | / | %
power->bitwisenot^power | bitwisenot
bitwisenot->not bitwisenot | factor
factor->(exp) | number | identifier

reg->regor {"|" regor}
regor->regand {"&" regand}
regand->regcl["#" | "?"]
regcl->"("reg")" | identifier

I found that: First(exp)={not, (, number, identifier} First(reg)=First(regor)=First(regand)=First(regcl)={(, identifier}

How can left common factors be extracted to make the grammar LL(1)?

0

There are 0 answers