Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<code>: := begin <stmts> end <stmts>: := <stmt> <terminator> | <stmts> <stmt> <t

ID: 3842519 • Letter: #

Question

<code>: := begin <stmts> end
<stmts>: := <stmt> <terminator>
| <stmts> <stmt> <terminator>
<stmt>: := print <expr>
<stmt>: : = input <var>
<stmt>: : = <if_stmt>
<stmt>: : = <loop>
<stmt>: : = <assign>
<stmt>: : = <declar>
<stmt>: : = <func>
<if_stmt>: := if <conds> <stmts> <optional_else> endif
<assign>: := <type> <identifier> ‘=’ <expr>
<declar>: := <type> <identifer>
<optional_else>: := <empty>
<stmt>: : = else <stmts>
<loop>: := <whileloop> | <forloop>
<whileloop>: := while ‘(‘ <conds> ‘)’ <stmts> endwhile
<forloop>: := for ‘(‘ <expr> ‘;’ <conds> ‘;’ <expr> ) <stmts> endfor
<func>: := function <type> <identifer>(<params>)
<params>: := {<type> <identifier>}{0,10}
<expr>: := <expr> ‘+’ <expr>
| <expr> ‘-‘ <expr>
| <expr> ‘*’ <expr>
| <expr> ‘/‘ <expr>
| <expr> ‘%’ <expr>
| ‘-‘ <expr>
| ‘(‘ <expr> ‘)’
| <identifer>
| <num>
| <identifier>’++’
| ‘++’<identifer>
| <identifer>’--’
| ‘—-‘<identifer>
| <str>
| <funccall>
<funcall>: := <identifer>(paramslist)
<paramlist>: := {<expr>}{0,10}
<conds>: := [!]<cond> {{&& | ||} [!]<cond>}*
<cond>: := <expr> == <expr>
| <expr> > <expr>
| <expr> < <expr>
| <expr> != <expr>
| <expr> >= <expr>
| <expr> <= <expr>
| ! <expr>
| ‘(‘ <cond> ‘)’
<empty>: :=
<num>: := [0-9]+
<str>: := "([^"]|\")*"
<identifier>: := [a-zA-Z_][a-zA-Z0-9_]{0,200}
<terminator>: := [;]+
<type>: := bool|char|string|int|double

I need to implement this BNF grammer using lex and yac tool. If somebody can help me to start this or to give an idea about its implementation.

Explanation / Answer

<code>: := begin <stmts> end

<stmts>: := <stmt> <terminator>

<stmts> <stmt> <terminator>

<stmt>: := print <expr>

<stmt>: : = input <var>

<stmt>: : = <if_stmt>

<stmt>: : = <loop>

<stmt>: : = <assign>

<stmt>: : = <declar>

<stmt>: : = <func>

<if_stmt>: := if <conds> <stmts> <optional_else> endif

<assign>: := <type> <identifier> ‘=’ <expr>

<declar>: := <type> <identifer>

<optional_else>: := <empty>

<stmt>: : = else <stmts>

<loop>: := <whileloop> | <forloop>

<whileloop>: := while ‘(‘ <conds> ‘)’ <stmts> endwhile

<forloop>: := for ‘(‘ <expr> ‘;’ <conds> ‘;’ <expr> ) <stmts> endfor

<func>: := function <type> <identifer>(<params>)

<params>: := {<type> <identifier>}{0,10}

<expr>: := <expr> ‘+’ <expr>

<expr> ‘-‘ <expr>

<expr> ‘*’ <expr>

<expr> ‘/‘ <expr>

<expr> ‘%’ <expr>

<expr>

‘(‘ <expr> ‘)’

<identifer>

<num>

<identifier>’++’

‘++’<identifer>

<identifer>’--’

<identifer>

<str>

<funccall>

<funcall>: := <identifer>(paramslist)

<paramlist>: := {<expr>}{0,10}

<conds>: := [!]<cond> {{&& | ||} [!]<cond>}*

<cond>: := <expr> == <expr>

<expr> > <expr>

<expr> < <expr>

<expr> != <expr>

<expr> >= <expr>

<expr> <= <expr>

! <expr>

(‘ <cond> ‘)’

<empty>: :=

<num>: := [0-9]+

<str>: := "([^"]|\")*"

<identifier>: := [a-zA-Z_][a-zA-Z0-9_]{0,200}

<terminator>: := [;]+

<type>: := bool|char|string|int|double

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote