A slr, LR, and lalr syntax analyzer

Source: Internet
Author: User

These days I have compiled some syntax analyzers that can analyze statements that comply with SLR, LR, and lalr specifications. All classes are placed in the syntexparse package. The algorithm comes from longshu. Below are some usage instructions.

The symbol class indicates the Terminator and non-terminator in the grammar.

The first parameter of the constructor is the symbolic name and the type is string. The second parameter indicates whether the symbol is a terminator or a non-Terminator. The type is boolean.
Usage: for the derivation rule E-> E + T Chinese method symbol E, +, t can be expressed in this way
Non-terminator E: Symbol Expression = new symbol ("e", false );
Terminator +: symbol plus = new symbol ("+", true );
If the second parameter of the symbol constructor is false, it indicates that the symbol is not a Terminator, and vice versa.
The second parameter can be omitted for non-terminator definitions, just like
Non-terminator T: Symbol term = new symbol ("T ");

 

The Production Class indicates the deduction rules of grammar.

The first parameter of the constructor indicates the header of the rule. The type is symbol. The second parameter indicates the rule body, type: Symbol [].
Usage: The preceding deduction rule E-> E + t can be expressed as follows:
Production P = new production (expression, new symbol [] {expression, plus, term });

 

Lrmaker, slrmaker, and lalrmaker are used to construct LR (1), slr, and lalr analysis tables respectively. They implement the parsergenerator interface.

This is the parsergenerator interface:

Package syntexparse; <br/> Import Java. util. *; <br/> Public interface parsergenerator {<br/> Public Map <string, integer> getgototable (); <br/> Public Map <string, Action> getactiontable () throws conflictexception; <br/> public production getproduction (int I); <br/>}

For the following analysis table

Getgototable: return the Goto table in the analysis table, and the action table in getactiontable. I used map to represent the analysis table. A string consisting of State + grammar symbols in the table is used as the key value. Therefore, to obtain the status 0 and run the symbol C, call actiontable. Get (0 + "C ").

You can use the slrgenfactory, lrgenfactory, and lalrgenfactory classes to generate analysis tables meeting the parsergenerator interface. The construction classes include lrmaker, slrmaker, and lalrmaker. For example, to construct the LR analysis table of the following syntax:

S'-> S
S-> CC
C-> CC | D

Symbol _ S = new symbol ("S"); <br/> symbol S = new symbol ("S "); <br/> symbol C = new symbol ("C"); <br/> symbol C = new symbol ("C", true ); <br/> symbol d = new symbol ("D", true ); <br/>/* the deduction rule starting with the start symbol must be placed in the first */<br/> production [] productions = new production [] {New Production (_ s, new symbol [] {s}), </P> <p> New Production (s, new symbol [] {C, C }), </P> <p> New Production (C, new symbol [] {C, C}), </P> <p> New Production (C, new symbol [] {d}) </P> <p> }; <br/>/* The start symbol of the grammar must be placed in the first */<br/> symbol [] symbols = new symbol [] {_ S, S, C, C, d };</P> <p> lrgenfactory fa = new lrgenfactory (); <br/> parsergenerator Pg = fa. getparsergenerator (productions, symbols); </P> <p> // output analysis table <br/> system. out. println (PG. getactiontable (); <br/> system. out. println (PG. getgototable ());

 

Parser class uses analysis tables for syntax analysis.
Use parser to analyze the following syntax:
E '-> E
E-> E + T | T
T-> T * f | f
F-> (e) | ID

Symbol _ e = new symbol ("E'"); <br/> symbol E = new symbol ("e "); <br/> symbol t = new symbol ("T"); <br/> symbol F = new symbol ("F "); <br/> symbol plus = new symbol ("+", true); <br/> symbol multiple = new symbol ("*", true ); <br/> symbol Lp = new symbol ("(", true); <br/> symbol Rp = new symbol (")", true ); <br/> symbol ID = new symbol ("ID", true); </P> <p> production [] productions = new production [] {New Production (_ e, new symbol [] {e}), </P> <p> new production (E, new symbol [] {e, plus, t }), </P> <p> new production (E, new symbol [] {t}), </P> <p> New Production (T, new symbol [] {T, multiple, f}), </P> <p> New Production (T, new symbol [] {f}), </P> <p> New Production (F, new symbol [] {LP, E, RP}), </P> <p> New Production (F, new symbol [] {ID }) </P> <p >}; <br/> symbol [] symbols = new symbol [] {_ E, E, T, F, plus, multiple, LP, RP, id }; <br/> // use lalr syntax analysis <br/> parser lalrparser = new Parser (productions, symbols, new lalrgenfactory ()); </P> <p> // use LR (1) syntax analysis <br/> parser lrparser = new Parser (productions, symbols, new lrgenfactory ()); </P> <p> // use SLR syntax analysis <br/> parser slrparser = new Parser (productions, symbols, new slrgenfactory ()); </P> <p> // perform syntax analysis <br/> lalrparser. parse (); <br/>

 

The analysis process of (ID + id) * ID $ is as follows:

C:/> javac parser. Java
Run the parser Class C:/> JAVA syntexparse. parser and press enter to separate each input symbol.


When a common expression is encountered, the Protocol step is output.

 

Source code can be downloaded here

Download csdn

Google download

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.