Sample Language Context Free Grammar

<expression> :

<expression>-<arithmetic expression>|<relational expression>|<Boolean expression>|<assignment expression>

<arithmetic expression>

<arithmetic expression> -> <arithmetic expression> + <term> | <arithmetic expression> - <term>|<term>

<term> -> <term>* <factor>|<term>/<factor>|<term>%<factor><factor>

<factor> -> (<arithmetic expression>)<constant>|<variable>|<function call>

<constant> -> <numeric constant> <character constant>

<variable> -> <identifier>

<function call> -> <identifier>(<argument list>)

<argument list> -> <argument>|e

<actual parameter> -> <expression>|<expression>,<actual parameter>

<relational expression>

<relational expression> -> <arithmetic expression><relational operator>arithmetic expression>

<relational operator> -> > |<|>= l <= | == | !=

<Boolean expression>

<Boolean expression> -> <Boolean expression> || <Boolean term>|<Boolean term>

<Boolean term> -> <Boolean term> && <Boolean factor>|<Boolean factor>

<Boolean Factor> -> <Arithmetic Expression>|<Relational Expression>|!<Boolean Expression>

<assignment expression>

<assignment expression> -> <identifier>=<expression>

<statement>

<statement> -> <declaration statement>|<execution statement>

<declaration statement>

<declaration statement> -> <value declaration>|<function declaration>|e

<value declaration> -> <constant declaration>|<variable declaration>

<constant declaration> -> const <constant type><constant declaration table>

<constant type> -> int | char | float

<constant declaration> -> <identifier> = <constant>;|<identifier> = <constant>,<constant declaration>

<variable declaration> -> <variable type> <variable declaration table>

<variable declaration table> -> <single variable declaration>;|<single variable declaration>,<variable declaration table>

<single variable declaration> -> <variable>|<variable> = <expression>

<variable type> -> int | char | float

<function declaration> -> <function type> <identifier>(<function declaration parameter list>);

<function type> -> int | char | float | void

<function declaration parameter list> -> <function declaration parameter>|e

<function declaration parameter> -> <variable type>|<variable type>,<function declaration parameter>

<execute statement>

<execution statement> -> <data processing statement>|<control language>|<compound language>

<data processing statement> -> <assignment statement>|<function call statement>

<assignment statement> -> <assignment expression>;

<function call statement>-<function call>;

<control statement> -> <if statement>|<for statement>|<while statement> | <do while statement> l <return statement>

<compound statement> -> {<statement list>}

<statement list> -> <statement> | <statement><statement list>

<if statement> -> if (<expression>)<statement>| if (<expression>)<statement> else <statement>

< for statement> -> for (<expression>;<expression>;<expression>)<loop statement>

<while statement> -> while (<expression>)<loop statement>

< do while statement> -> do<looping compound> while (<expression>);

<loop statement> -> <declaration statement>|<loop execution statement> | <compound statement for loop>

<compound statement for loop> -> {<loop statement list>}

<loop statement> -> <loop statement>|<loop statement><loop statement>

<loop execution statement> -> <if statement for loop>|<for statement>|<while statement><do while statement><return statement>l<break statement>l<continue statement>

<if statement for loop> -> if (<expression>)<loop statement>|if (<expression>)<loop statement> else <loop statement>

<return statement> -> return; | return <expression>;

<break statement> -> break;

<continue statement> -> continue;

<function definition>

<function definition> -> <function type> <identifier>(<function definition parameter list>) <compound statement>

<function definition parameter list> -> <function definition parameter>|e

<function definition parameter> -> <variable type> <identifier>|<variable type> <identifier>,<function definition parameter>

<program>

<program> -> <declaration statement> main() <compound statement> function block>

<function block> -> <function definition><function block>|e

Guess you like

Origin blog.csdn.net/QRLYLETITBE/article/details/130376770