1 23 24 package net.percederberg.grammatica.test; 25 26 import java.io.Reader ; 27 28 import net.percederberg.grammatica.parser.Analyzer; 29 import net.percederberg.grammatica.parser.ParserCreationException; 30 import net.percederberg.grammatica.parser.ProductionPattern; 31 import net.percederberg.grammatica.parser.ProductionPatternAlternative; 32 import net.percederberg.grammatica.parser.RecursiveDescentParser; 33 34 40 class ArithmeticParser extends RecursiveDescentParser { 41 42 50 public ArithmeticParser(Reader in) throws ParserCreationException { 51 super(new ArithmeticTokenizer(in)); 52 createPatterns(); 53 } 54 55 64 public ArithmeticParser(Reader in, Analyzer analyzer) 65 throws ParserCreationException { 66 67 super(new ArithmeticTokenizer(in), analyzer); 68 createPatterns(); 69 } 70 71 77 private void createPatterns() throws ParserCreationException { 78 ProductionPattern pattern; 79 ProductionPatternAlternative alt; 80 81 pattern = new ProductionPattern(ArithmeticConstants.EXPRESSION, 82 "Expression"); 83 alt = new ProductionPatternAlternative(); 84 alt.addProduction(ArithmeticConstants.TERM, 1, 1); 85 alt.addProduction(ArithmeticConstants.EXPRESSION_REST, 0, 1); 86 pattern.addAlternative(alt); 87 addPattern(pattern); 88 89 pattern = new ProductionPattern(ArithmeticConstants.EXPRESSION_REST, 90 "ExpressionRest"); 91 alt = new ProductionPatternAlternative(); 92 alt.addToken(ArithmeticConstants.ADD, 1, 1); 93 alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1); 94 pattern.addAlternative(alt); 95 alt = new ProductionPatternAlternative(); 96 alt.addToken(ArithmeticConstants.SUB, 1, 1); 97 alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1); 98 pattern.addAlternative(alt); 99 addPattern(pattern); 100 101 pattern = new ProductionPattern(ArithmeticConstants.TERM, 102 "Term"); 103 alt = new ProductionPatternAlternative(); 104 alt.addProduction(ArithmeticConstants.FACTOR, 1, 1); 105 alt.addProduction(ArithmeticConstants.TERM_REST, 0, 1); 106 pattern.addAlternative(alt); 107 addPattern(pattern); 108 109 pattern = new ProductionPattern(ArithmeticConstants.TERM_REST, 110 "TermRest"); 111 alt = new ProductionPatternAlternative(); 112 alt.addToken(ArithmeticConstants.MUL, 1, 1); 113 alt.addProduction(ArithmeticConstants.TERM, 1, 1); 114 pattern.addAlternative(alt); 115 alt = new ProductionPatternAlternative(); 116 alt.addToken(ArithmeticConstants.DIV, 1, 1); 117 alt.addProduction(ArithmeticConstants.TERM, 1, 1); 118 pattern.addAlternative(alt); 119 addPattern(pattern); 120 121 pattern = new ProductionPattern(ArithmeticConstants.FACTOR, 122 "Factor"); 123 alt = new ProductionPatternAlternative(); 124 alt.addProduction(ArithmeticConstants.ATOM, 1, 1); 125 pattern.addAlternative(alt); 126 alt = new ProductionPatternAlternative(); 127 alt.addToken(ArithmeticConstants.LEFT_PAREN, 1, 1); 128 alt.addProduction(ArithmeticConstants.EXPRESSION, 1, 1); 129 alt.addToken(ArithmeticConstants.RIGHT_PAREN, 1, 1); 130 pattern.addAlternative(alt); 131 addPattern(pattern); 132 133 pattern = new ProductionPattern(ArithmeticConstants.ATOM, 134 "Atom"); 135 alt = new ProductionPatternAlternative(); 136 alt.addToken(ArithmeticConstants.NUMBER, 1, 1); 137 pattern.addAlternative(alt); 138 alt = new ProductionPatternAlternative(); 139 alt.addToken(ArithmeticConstants.IDENTIFIER, 1, 1); 140 pattern.addAlternative(alt); 141 addPattern(pattern); 142 } 143 } 144 | Popular Tags |