Parse EBNF/Tests

From Rosetta Code
Revision as of 22:43, 22 May 2011 by Underscore (talk | contribs) (Created page with "The flavor of EBNF used here is the same as that defined [http://karmin.ch/ebnf/index here], except a literal can't contain the character used for quoting, and an identifier can'...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

<lang ebnf>"a" {

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

} "z"</lang>

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

<lang ebnf>{

   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" .

}</lang>

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

<lang ebnf>a = "1";</lang>

<lang ebnf>{ a = "1" ;</lang>

<lang ebnf>{ hello world = "1"; }</lang>

<lang ebnf>{ foo = bar . }</lang>