JAVACC learning notes

xiaoxiao2021-03-06  115

The functionality of JavaCC is similar to YACC, mainly based on the BNF paradigm, but Javacc is a collection of lexical analysis and grammatical analysis to generate Java resolution code. Home is: https: //javacc.dev.java.net/

Javacc has three tools

Javacc is used to handle syntax files (JJ) to generate parsing code;

JJTREE is used to process JJT files, generate tree node code and JJ files, and then generate parsing code through JAVACC;

JJDOC generates BNF Paradigm Document (HTML) according to JJ files

Javacc's grammar files in various languages ​​used here http://www.cobase.cs.ucla.edu/pub/javacc/, such as HTML, XML, Python, VB ...., a lot of enough, huh, huh.

Javacc

In Javacc, the most important thing is "grammar" .java this is the main program of the parser, "Grammar" name is defined by JJ.

Now explain the definition of the JJ file according to the example:

BNF paradigm is:

EXPRESSION

:: =

() * simple_expression ) *

Simple_expression

:: =

Term (Addop Term) *

Addop

:: =

|

Term

:: =

Factor (Mulop Factor) *

Mulop

:: =

|

Factor

:: =

|

|

|

|

Simple_Expression

/ * This is an example of an integer of four operations * /

/ * Run javacc grammar.jj

Javac * .java

Java grammar

>>> 1 1 * (1 1)

3

>>> ^ z

* /

Parser_Begin (grammar) / * Analyze the entry of the code * /

Public class grammar {

Public static final int plusop = 1;

Public static final int minusop = 2;

Public Static Final Int Timersop = 3;

Public static final int overop = 4;

Public static void main (string args []) THROWS PARSEEXCEPTION {

Grammar parser = new grammar (system.in);

Parser.Expression ();

}

}

Parser_END (Grammar)

Skip: / * Do not processed characters * /

{

"" | "/ t"

}

Token: / * Generate the character definition of Token * /

{

| |

|

|

|

| >

|

|

}

Void Expression ():

/ * Complete Expression :: = () * Simple_expression ) *

{

INT value = 0; / * This {} is a local variable for expression (). * /

}

{

{

System.out.print (">>>");

}

( / * first matches newline this Taken, turn to the next resolution after completion * /

{

System.out.print ("> "); / * In , if you match the Java code executed by . * /

}

) * value = simple_expression () / * Before the wrap, simple_expression () parsing the expression, after entering the wrap, a budget analysis completed * /

{System.out.println (Value);

System.out.print (">>>"); / * In , the Java code executed in in {} under . * /

}

) *

/ * System defined Taken, input end value * /

}

INT Simple_Expression ():

/ * Complete Simple_Expression :: = BNF Term (Addop Term) * Found with * /

{

/ * This {} is a local variable defined in simple_expression () * /

Int value;

Int TValue;

INT OP;

}

{

Value = term () {} / * with Term phase * /

(

Op = addop () TVALUE = TERM ()

{

Switch (OP)

{

Case plusop:

Value = Value TValue;

Break;

Case minusop:

Value = value - TVALUE

Break;

}

}

) * / * Match (Addop Term) * /

{Return Value;

}

Int addop (): {}

{

{return plusop;}

| {return minusop;}

}

Int term ():

{

Int value;

Int TValue;

INT OP;

}

{

Value = factor () {}

(

Op = mulop () TVALUE = Factor ()

{

Switch (OP)

{

Case Timersop: Value = Value * TValue;

Break;

Case Overop:

Value = Value / TValue;

Break;

}

}

) *

{

Return Value;

}

}

Int mulop (): {}

{

{RETURN TIMERSOP;}

| {return};

}

Int factor ():

{

Int value;

Token T;

}

{

T = / * Gets the value of the resolution of * /

{

Value = 100;

Return Value;

}

|

T =

{

Value = (Integer.Valueof (t.image)). INTVALUE ();

Return Value;

}

|

T =

{

Value = 0-factor ();

Return Value;

}

|

T =

{

VALUE = Factor ();

Return Value;

}

|

value = Simple_expression ()

{

Return Value;

}

}

According to an example: Basically, it is a Java code that is used to handle the current Tabkn.

JJTree's use:

JJTree's use, you need to write your own Node class according to the actual situation, but you must implement the node.java interface, JJTree provides a simple implementation of SimpleNode.java, or inherits it, or rewrites this class.

Give a Javacc you have an example and four operations:

Syntax definition:

Start

:: =

Expression ";"

EXPRESSION

:: =

AdditiveExpression

AdditiveExpression

:: =

(MultiplicativeExpression ((" " | "-") MultiplicativeExpression *)

MultiPlicativeExpression

:: =

(UnaryExpression (("*" | "/" | ") unaryexpression *)

UnaryExpression

:: =

"" Expression ")"

|

Identifier

|

Integer

Identifier

:: =

Integer

:: =

Options {

Multi = True;

Visitor = true; / * Implement matching Visitor mode code * /

Node_Default_void = true; / * Resolution Function The default does not generate the Node class * /

}

/ * JTT default generated Node class name, all with AST prefix plus the name of the current resolution * /

Parser_Begin (EG4)

Class eg4 {

Public static void main (string args []) {

System.out.println ("

Reading

From standard input ... ");

EG4 T = New EG4 (system.in); try {

ASTSTART N = T.Start ();

Eg4visitor v = new eg4dumpvisitor ();

n.jjtaccept (v, null);

System.out.println ("Thank you.");

} catch (exception e) {

System.out.println ("OOPS.");

System.out.println (E.getMessage ());

E.PrintStackTrace ();

}

}

}

Parser_END (EG4)

SKIP:

{

""

| "/ t"

| "/ n"

| "/ r"

<"//" (~ "/ n", "/ r"]) * ("/ n" | "/ r" | "/ r / n")>

<"/ *" (~ ["*"]) * "*" (~ ["/"] (~ ["*"]) * "*") * "/">

}

Token: / * Literals * /

{

(["L", "L"])?

| (["L", "L"])?

| (["L", "L"])?

>

|

<#Decimal_litral: ["1" - "9"] ([0 "-" 9 "]) *>

|

<# Hex_literal: "0" ["X", "X"] ([0 "-" 9 "," A "-" f "," a "-" f "]) >

|

<#Octal_literal: "0" (["0" - "7"]) *>

}

Token: / * Identifiers * /

{

( | ) *>

|

<# Loading: ["_", "a" - "z", "a" - "z"]>

|

<#Digit: ["0" - "9"]>

}

ASTSTART Start () #start: {} / * #start Generates the defined node class, name is prefix start.java * /

{

Expression () ";"

{return jjtthis;}

}

Void Expression (): {}

{

AdditiveExpression ()

}

Void AdditiveExpression (): {}

{

(

MultiplicativeExpression () ((" " | "-") MultiplicativeExpression ()) *

#Add (> 1) / * Add # When condition (> 1), add generates defined node class, name is prefix add.java * /}

Void MultiplicativeExpression (): {}

{

(

UnaryExpression () (("*" | "/" | "%") unaryexpression ()) *

#Mult (> 1) / * # MULT When the condition (> 1), Mult generates the defined node class, the name is prefix mult.java * /

}

Void unaryExpression (): {}

{

"(" Expression () ")" | Identifier () | Integer ()

}

Void identifier () #myotherid: / * # myotherid generates the defined node class, name is prefix myotherid.java * /

{

Token T;

}

{

T =

{

Jjtthis.setname (t.image);

}

}

Void Integer () #integer: {} / * # integer generates the defined node class, name prefix integer.java * /

{

}

JJDOC is very simple.

If you need to generate other languages ​​(eg, C #) parsers, Antlr (http://www.antlr.org/) is also a good choice in addition to the Yacc and Lex of C.

转载请注明原文地址:https://www.9cbs.com/read-103278.html

New Post(0)