/****************************************************************** This program implements and tests a recursive descent parser for a language defined by a grammar given below. Testing is done by constructing a ParseTree and inspecting the result by calling the functions "traverse" and "displayYield". The ParseTree construtor takes as its argument the name of a text file. It attempts to parse the text as a program in the language generated by the grammar. If the parse is successful, a representation of a parse tree is returned. Otherwise an error message is printed, and a dummy tree is returned. The function "traverse" simply prints the tree if there is one (i.e., if its input is not a dummy). Otherwise it prints an error message. The function "displayYield" returns a list of the tokens in the leaves of a parse tree (from left to right). This should be the list of tokens in the string that was parsed. The "main" function simply tests the functions by calling it with a number of different text files. Each file is expected to be in the current directory. ******************************************************************/ // The grammar for the language is given below. Here nonterminals are // capitalized. The grammar has been expanded so that in each rule, // only tokens appear on the RHS of the arrow. // The start symbol is "Conditional". // Conditional -> ShortIf RestIf // ShortIf -> if Test then Block // RestIf -> else Block endif // RestIf -> endif // Test -> < Var Op Var> // Var -> x // Var -> y // Var -> z // Op -> = // Op -> /= // Block -> begin RestBlock // RestBlock -> end // RestBlock -> Statement Statements // Statements -> end // Statements -> Statement Statements // Statement -> Var := Var ////////////////////////////////// The "main" function //////////////////////////////////////////////////// class A4 { public static void main(String args[]) { ParseTree t; t=new ParseTree("data1.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data2.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data3.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data4.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data5.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data6.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data7.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data8.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data9.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data10.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data11.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data12.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data13.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data14.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data15.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data16.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); t=new ParseTree("data17.txt"); t.traverse(); System.out.println(); t.displayYield(); System.out.println(); } }