In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form
A => a
a: terminals
Abstraction: <assign>
is a abstraction
<assign> -> <var> = <expression>
Example production:
<assignment> -> tID tASSGN tID; // For first assignment statement
<assignment> -> tID = tID + tID; <-- Production 1
| tId = tID; <-- Production 2
But also we can write some type statement x = y + z * x
So we should have write our production to handle more complex statements. So recursion can help us.
<assignment> -> tID = <expr>
<expr> -> tID // Recursive role
| <expr> + <expr>
| <expr> * <expr>
<program>
a start symbol. Sentential forms can be :begin <stmt_list> end OR
begin <stmt> ; <stmt_list> end
==>
to express derivation.begin <stmt list> end ==> begin <stmt> ; <stmt list> end
<program> ==> begin <var> = <expr> end
==> begin tIDENT = <expr> end
==> begin tIDENT = <var> + <var> end
==> begin tIDENT = tIDENT + <var> end
......
<program> ==> begin <var> = <expr> end
==> begin <var> = <var> + <var> end
==> begin <var> = <var> + tIDENT end
==> begin <var> = tIDENT + tIDENT end
......
Hierarchy of syntactic structure of the sentence:
begin A = C + B end
Yield: How parser read labels in this example (A - C - B
)
A = B + C * A
<assign> -> tIDENT = <expr>
<expr> -> <expr> + <expr>
| <expr> * <expr>
| tIDENT
*
has a higher priority than the +
or -
.A - B - C
<assign> -> tID = <expr>
<expr> -> <expr> + <term> // Lowe priority root tree'ye yakın olan
| | | ==> left recursive rule left associativty
¯¯¯¯¯¯¯¯¯¯
| <term>
==> <term> + <term> ...
<term> -> <term> * tID
| tID
==> <tID> + <tID> ...
This is dangling else problem. We don't know else is belong to which if statement.
Our grammer:
<if> -> if <bool> then <stats>
| if <bool> then <stats> else <stats>
<stats> -> <if> | <asgn> | <while> ....
The solution: else is belong to nearest if statement. So we need to modify our grammer.
<unmatched> -> if <bool> then <stats>
<matched> -> if <bool> then <matched> else <stats>
| <assgn>
| <for>
<stats> -> <matched> | <unmatched>