Parse EBNF/Tests

From Rosetta Code

The flavor of EBNF used here is the same as that defined here, except a literal can't contain the character used for quoting, and an identifier can't contain whitespace or any of the characters = | ( ) { } [ ] . ; " '.

A one-liner[edit]

"a" {
a = "a1" ( "a2" | "a3" ) { "a4" } [ "a5" ] "a6" ;
} "z"

Some valid inputs:

  • a1a3a4a4a5a6
  • a1 a2a6
  • a1 a3 a4 a6

Some invalid inputs:

  • a1 a4 a5 a6
  • a1 a2 a4 a5 a5 a6
  • a1 a2 a4 a5 a6 a7
  • your ad here

Arithmetic expressions[edit]

{
expr = term { plus term } .
term = factor { times factor } .
factor = number | '(' expr ')' .
 
plus = "+" | "-" .
times = "*" | "/" .
 
number = digit { digit } .
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
}

Some valid inputs:

  • 2
  • 2*3 + 4/23 - 7
  • (3 + 4) * 6-2+(4*(4))

Some invalid inputs:

  • -2
  • 3 +
  • (4 + 3

Some invalid EBNF[edit]

a = "1";
{ a = "1" ;
{ hello world = "1"; }
{ foo = bar . }