/* Analyse syntaxique LL(1) par descente recursive déterministe */ public class AnalyseLL { // Analyseur et sortie en erreur de syntaxe static public void main(String[] args) { lireAxiome(); System.out.println(" Syntax OK"); } static void erreur() { throw new java.lang.RuntimeException("Syntax Error"); } // Tokens enum Tok {EOF, IDENT, INT, PLUS, MOINS, MULT, DIV, LPAR, RPAR} // Lexer static Tok input[]= {Tok.INT , Tok.MOINS, Tok.IDENT, Tok.MULT, Tok.INT, Tok.MOINS, Tok.INT, Tok.EOF}; static int index=0; static void lireTok(Tok t) { if ( input[index++] != t ) erreur(); } static Tok aheadTok() { return input[index]; } // Parser /* grammaire calculatrice : Axiome = Exp EOF Exp = Terme SuiteTerme ; Terme = Facteur SuiteFacteur ; SuiteTerme = Epsilon | + Terme SuiteTerme| - Terme SuiteTerme SuiteFacteur = Epsilon | * Facteur SuiteFacteur | / Facteur SuiteFacteur Facteur = IDENT | INT | '(' Exp ')' */ static void lireAxiome() { lireExp(); lireTok(Tok.EOF); } static void lireExp() { lireTerme(); lireSuiteTerme(); } static void lireTerme() { lireFacteur(); lireSuiteFacteur(); } static void lireSuiteTerme() { switch (aheadTok()) { case PLUS : lireTok(Tok.PLUS); lireExp(); break; case MOINS : lireTok(Tok.MOINS); lireExp(); break; default: /* lireVide() */ break; } } static void lireSuiteFacteur() { switch (aheadTok()) { case MULT : lireTok(Tok.MULT); lireTerme(); break; case DIV : lireTok(Tok.DIV); lireTerme(); break; default: /* lireVide() */ break; } } static void lireFacteur() { switch (aheadTok()) { case IDENT: lireTok(Tok.IDENT); break; case INT : lireTok(Tok.INT); break; case LPAR : lireTok(Tok.LPAR); lireExp(); lireTok(Tok.RPAR); break; default: erreur(); break; } } }