|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.ObjectGrammar
This class implements context-free grammars.
The productions of the grammar are stored
in an array productionsArray. Each production is an
object of the class Production. There are separated
alphabets terminals for terminals and variables
for variables plus an alphabet alphabet for their union.
The usual functions First() and Follow()
are implemented as methods of this class. The grammar can be read
from a file where the productions are listed one by line in the form
l:r with a ":" separating the two sides of each production.
The initial rule should be the last one. The following grammars are included
in the directory Data:
ETF.txt the ordinary ETF grammar,
which is SLR but not LL(1).
ETFPrime.txt the variant of the ETF grammar which is LL(1)
obtained by eliminating left recursion.
Dyck.txt the grammar of the Dyck language, which is LL(1).
| Field Summary | |
Alphabet |
alphabet
The total alphabet. |
java.util.Set[] |
derive
The set of rules with given left symbol. |
boolean[] |
epsilon
The erasable symbols. |
int |
initial
The initial production. |
int |
lgProductions
The sum of lengths of productions . |
int |
nbProductions
The number of productions. |
Production[] |
productionsArray
The array of grammar productions. |
Alphabet |
terminals
The terminals. |
Alphabet |
variables
The variables. |
| Constructor Summary | |
Grammar(int n)
Creates a grammar with n symbols. |
|
| Method Summary | |
static Grammar |
Dyck()
The Dyck grammar: S -> (S)S | "". |
void |
epsilon()
Implements Epsilon(). |
static Grammar |
ETF()
The ETF grammar : E -> E + T T -> T * F F -> (E) | char . |
static Grammar |
ETFPrime()
The modified version of the ETF grammar which
is LL(1) : E -> Te e -> +Te | "" T -> Ft t -> *Ft | "" F -> (E) | char . |
void |
exploreFirstChild(short v,
boolean[] mark)
Explores the graph of First. |
void |
exploreSibling(short v,
boolean[] mark)
Explores the graph of Follow(). |
java.util.Set |
first(short[] s)
Extends the function First() to Strings. |
java.util.Set |
firstChild(short v)
Implements the fuction FirstChild(). |
java.util.Set |
follow(short v)
Implements the function Follow(). |
static Grammar |
fromFile(java.lang.String name)
|
void |
initAlphabet()
Initializes alphabet, variables and terminals. |
void |
initGrammar()
Computes derive, alphabet and epsilon. |
static void |
main(java.lang.String[] args)
|
java.util.Set |
sibling(short v)
Implements the function Sibling(). |
java.lang.String |
toString()
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
public Production[] productionsArray
public int nbProductions
public int lgProductions
public boolean[] epsilon
public java.util.Set[] derive
public Alphabet variables
public Alphabet terminals
public Alphabet alphabet
public int initial
I -> E$.
| Constructor Detail |
public Grammar(int n)
n symbols.
| Method Detail |
public static Grammar fromFile(java.lang.String name)
throws java.io.IOException
java.io.IOExceptionpublic void epsilon()
Epsilon(). Initializes the boolean
array epsilon of erasable symbols.
public java.util.Set firstChild(short v)
FirstChild().
v - a letter (variable or terminal)
v in the graph First
(terminals and variables).
public void exploreFirstChild(short v,
boolean[] mark)
v - a letter (variable or terminal)mark - the array of marks used for the exploration.public java.util.Set first(short[] s)
First() to Strings.
public java.util.Set sibling(short v)
Sibling().
v - a variable
Sibling(v) of sucessors of the
variable v in the graph of Follow().
public void exploreSibling(short v,
boolean[] mark)
Follow().
v - a variablepublic java.util.Set follow(short v)
Follow().
v - a variable
Follow(v)public static Grammar Dyck()
S -> (S)S | "".
public static Grammar ETF()
ETF grammar : E -> E + T T -> T * F F -> (E) | char .
public static Grammar ETFPrime()
ETF grammar which
is LL(1) : E -> Te e -> +Te | "" T -> Ft t -> *Ft | "" F -> (E) | char .
public void initAlphabet()
public void initGrammar()
public java.lang.String toString()
public static void main(java.lang.String[] args)
throws java.io.IOException
java.io.IOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||