Compilation principle Syntax analysis Experiment (Java Implementation)

Source: Internet
Author: User

Import Java.util.stack;public class LL1 {//Add sync symbol ll (1) Parse table private String [] analysistable = new string[][]{{"TZ", "" "" " , "TZ", "Synch", "Synch"},{"", "+tz", "", "", "ε", "ε"},{"FY", "Synch", "", "FY", "Synch", "Synch"},{"", "ε", "*fy", "", "ε", "ε "},{" I "," Synch "," Synch "," (E) "," Synch "," Synch "}};//store Terminator private String [] VT = new string[]{" I "," + "," * "," (",") "," # " };//store Terminator private string [] VN = new string[]{"E", "Z", "T", "Y", "F"};//input string private StringBuilder Strtoken = new StringBuilder (") i*+i");//analysis stack private stack<string> stack = new stack<string> ();//a save an input symbol read from the input string, the current symbol private String a  = null;//x holds stack stack top symbol private String X = Null;//flag Flag Predictive Analysis succeeded private Boolean flag = true;//Record the position of the current character in the input string private int cur = 0;//record steps private int count = 0;//initialize protected void init () {strtoken.append ("#"); Stack.push ("#"); System.out.printf ("%-9s%-38s%6s%-20s\n", "step", "Symbol stack", "Input string", "used production"), Stack.push ("E"); Curcharacter (); System.out.printf ("%-6d%-20s%6s \ n", count,stack.tostring (), strtoken.substring (cur, STRTOKEN.LEngth ()));} Reads the current stack top symbol protected String Stackpeek () {X = Stack.peek (); return X;} Returns the letter of the current position in the input string, private String Curcharacter () {a = String.valueof (Strtoken.charat (cur)); return A;} Determine if X is Terminator protected Boolean XISVT () {for (int i = 0; i < (vt.length-1); i++) {if (Vt[i].equals (X)) {return true;}} return false;} Find X in non-terminator analysis table protected String VNTI () {int Ni = 0, Tj = 0;for (int i = 0; i < vn.length; i++) {if (Vn[i].equals (X)) {Ni = i;}} for (int j = 0; J < Vt.length; J + +) {if (Vt[j].equals (a)) {Tj = j;}} return ANALYSISTABLE[NI][TJ];} Judge M[a,a]={x->x1x2 ... xk}//put x1x2 ... XK Propulsion Stack//x1x2 ... xk=ε, do not push anything into the stack protected Boolean productiontype () {if (VNTI ()! = "") {return true;} return false;} Advance stack stack protected void Pushstack () {stack.pop (); String M = VNTI (); String ch;for (int i = (m.length ()-1), I >= 0; i--) {ch = string.valueof (M.charat (i)); Stack.push (ch);} System.out.printf ("%-6d%-20s%6s%-1s->%-12s\n", (++count), stack.tostring (), strtoken.substring (cur, Strtoken.length ()), x,m);} Master control program protected void toTalcontrolprogram () {while (flag = = True) {Stackpeek (); if (XISVT () = = True) {if (X.equals (a)) {cur++;a = Curcharacter (); Stack.pop (); System.out.printf ("%-6d%-20s%6s \ n", (++count), stack.tostring (), strtoken.substring (cur, strtoken.length ()));} Else{error ();}} else if (X.equals ("#")) {if (X.equals (a)) {flag = false;} Else{error ();}} else if (productiontype () = = True) {if (VNTI (). Equals ("synch")) {ERROR ();} else if (VNTI (). Equals ("ε")) {Stack.pop (); System.out.printf ("%-6d%-20s%6s%-1s->%-12s\n", (++count), stack.tostring (), strtoken.substring (cur, Strtoken.length ()), X,vnti ());} Else{pushstack ();}} Else{error ();}}} Error protected void error () {System.out.println ("An error occurred in the input string, unable to parse"); System.exit (0);} Print Storage analysis table protected void printf () {if (flag = = False) {System.out.println ("======== parse succeeded");} else {System.out.println ("======== analysis Failed");}} public static void Main (string[] args) {LL1 ll1 = new LL1 (); Ll1.init (); Ll1.totalcontrolprogram (); ll1.printf ();}}


Test Data 1:i*i+i

Test result 1:

Step        symbol Stack                                      input string  using the resulting formula               0      [#, E]               i*i+i# 1      [#, Z, T]            i*i+i# e->tz          2      [#, Z, Y, F]         i*i+i# t->fy          3      [#, Z, y, i]         i*i+i# f->i           4      [#, Z, y]             *i+i# 5      [#, Z, y, F, *]       *i+i# y->*fy         6      [#, Z, y, F]           i+i# 7      [#, Z, y, i]           i+i# f->i           8      [#, Z, y]
   
    +i# 9 [#,      z]                  +i# y->ε     [#, Z, T, +]            +i# z->+tz one     [#, Z, T]                i#     [#, Z, Y, F]             i# T->fy [#     , Z, y, i]             i# f->i     [#, Z, y]                 #     [#, z]                    # y->ε     [#]                       # z->ε           ======== Analysis Success
   

Test Data 2:) i*+i

Test Result 2:

Step        symbol Stack                                      input string  using the resulting formula               0      [#, E]               ) i*+i# input string error, unable to parse

Compilation principle Syntax analysis Experiment (Java Implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.