Search code examples
goebnf

Need an example for how Go syntax for assignment operator uses the grammar rules specified using EBNF


As mentioned in the docs, syntax in Go is specified using Extended Backus-Naur Form (EBNF):

Production  = production_name "=" [ Expression ] "." .
Expression  = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term        = production_name | token [ "…" token ] | Group | Option | Repetition .
Group       = "(" Expression ")" .
Option      = "[" Expression "]" .
Repetition  = "{" Expression "}" .

I am trying to understand how Go syntax grammar is defined, how to breakdown/derive/understand the expression i++ and i+=1 using these grammar rules. How would these production rules be substituted step by step for the purpose of illustration?


Solution

  • The expression i++ uses the grammar rule for IncDec statements:

    IncDecStmt = Expression ( "++" | "--" ) .
    

    Here, production_name would be IncDecStmt and Term would be "++" or "--".