O'Reilly logo

Building Parsers with Java™ by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.4. An Arithmetic Grammar Parser

You can create an arithmetic parser by translating the arithmetic grammar into a parser class and plugging in the assemblers. Section 3.6, “Translating a Grammar to Code,” gives a set of rules for creating a parser from a grammar. The only remaining problem is to prevent looping in the subparser definitions.

Section 6.5, “Eliminating Parser Class Loops,” explains how to address loops in a grammar. The arithmetic grammar has two cycles.

 expression = term (plusTerm | minusTerm)*; term = factor (timesFactor | divideFactor)*; plusTerm = '+' term; minusTerm = '-' term; factor = phrase expFactor | phrase; timesFactor = '*' factor; divideFactor = '/' factor; expFactor = '^' factor; phrase = '(' expression ')' | Num; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required