Here is the Task Objective: Objective
- Objective
For this task you will implement an LL(1) parser using pushdown automata (PDA) and predictive parsing tables. Given an input context-free grammar G = (V, E, R, S), along with the First and Follow sets for all rules, you need to
(i) construct the predictive parsing table for G,
(ii) construct the PDA equivalent to G,
(iii) implement an LL(1) parser for G which makes use of the table and the PDA to direct its decisions.
Given an input string w, the parser should signal an error if w L(G) and produce a derivation of w from S if w Є L(G). - Requirements.
• We make the following assumptions about input CFGS for simplicity.
(i) The set V of variables consists of upper-case English letters.
(ii) The start variable is the symbol S.
(iii) The set of terminals consists of lower-case English letters (except the letter e).
(iv) The letter “e” represents ɛ.
(v) We only consider LL(1) CFGs.
• You should implement a class constructor CfgL11Parser and a method parse.
• CfgL11Parser, a class constructor, takes one parameter which is a string description of a CFG, together with First and Follow sets for its rules, and constructs a CFG instance. A string encoding a CFG is of the form V#T#R#I#O.
(i) V is a string representation of the set of variables; a semicolon-separated sequence of upper-case English letters, starting with S.
(ii) T is a string representation of the set of terminals; a semicolon-separated sequence of alphabetically sorted lower-case English letters.
(iii) R is a string representation of the set of rules. R is a semicolon-separated sequence of pairs. Each pair represents the largest set of rules with the same left-hand side. Pairs are of the form i/j where i is a variable of V and j is a string representation of the set of right-hand sides a comma-separated sequence of strings. These pairs are sorted by the common left-hand side i based on the ordering of V.
(iv) I is a string representation of the First set of each rule. I is a semicolon-separated sequence of pairs. Pairs are of the form i/j where i is a variable of V and j is the string representation of the First sets of each right-hand side of a rule for i-a comma-separated sequence of strings. These sets appear in the same order of the corresponding rules and are concatenations of the symbols making up the represented set. These pairs are sorted by the common left-hand side i based on the ordering of V.
(v) O is a string representation of the Follow set of each variable. O is a semicolon- separated sequence of pairs. Pairs are of the form i/j where i is a variable of V and j is the string representation of the Follow set of i. These sets are encoded by concatenations of the symbols making up the represented set. These pairs are sorted by the common left-hand side i based on the ordering of V.
• For example, consider the CFG G1 = ({S, T}, {a, c, i}, R, S), where R is given by the following productions.
S i S T| ε
T c S | a
This CFG will have the following string encoding.
S; T#a; c; i#S/iST, e; T/cS, a#S/i, e; T/c, a#S/$ac; T/$ac
• parse takes an input string w and returns a string encoding a left-most derivation of w in G; in case w L(G), this derivation ends with “ERROR.” The parse method should construct a PDA equivalent to G and use the PDA together with the LL(1) parsing table to reach its decision. Note that we will be testing parse using only LL(1) grammars. Hence, you do not need to include a search algorithm in your implementation; w either has no derivation in G or has exactly one.
• A string encoding a derivation is a semicolon-separated sequence of items. Each item is a sentential form representing a step in the derivation. The first item is S. If wЄ L(G) the last item is w; otherwise, it is ERROR. For example, given G1, on input string iiac, parse should return the following string.
S; iST; iiSTT; iiTT; iiaT; iiacS; iiac
On the other hand, on input string iia, parse should return the following.
S; iST; iiSTT; iiTT; iiaT; ERROR
• Important Details:
(a) Your implementation should be done within the template file “CfgL11Parser.java❞.
(b) You are not allowed to change package, file, constructor, or method names/signatures.
(c) You are allowed to implement as many helper classes/methods within the same file (if needed).
CfgLl1Parser.java:
package csen1002.main.task8;
/**
* Write your info here
*
* @name Jane Smith
* @id 49-0234
* @labNumber 06
*/
public class CfgLl1Parser {
/**
* Constructs a Context Free Grammar
*
* @paramcfg A formatted string representation of the CFG, the First sets of
* each right-hand side, and the Follow sets of each variable. The
* string representation follows the one in the task description
*/
public CfgLl1Parser(String input) {
// TODO Auto-generated constructor stub
}
/**
* @param input The string to be parsed by the LL(1) CFG.
*
* @return A string encoding a left-most derivation.
*/
public String parse(String input) {
// TODO Auto-generated method stub
return null;
}
}
Task8TestsBatch.java:
package csen1002.main.task8;
import csen1002.main.task8.CfgLl1Parser;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.Timeout.ThreadMode;
@Timeout(value = 5, unit = TimeUnit.SECONDS, threadMode = ThreadMode.SEPARATE_THREAD)
public class Task8TestsBatch {
@Test
public void testCfg1String1() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;O;N;F;C#c;f;n;o;p;w;x#S/xSnO,nSOO,wC;O/xFw,nNx;N/wSO,oS,e;F/c,pC,e;C/fCxF,n#S/x,n,w;O/x,n;N/w,o,e;F/c,p,e;C/f,n#S/$nx;O/$nx;N/x;F/$nwx;C/$nwx");
assertEquals("S;nSOO;nnSOOOO;nnxSnOOOOO;nnxxSnOnOOOOO;nnxxxSnOnOnOOOOO;nnxxxwCnOnOnOOOOO;ERROR", cfgLl1Parser.parse("nnxxxwxn"));
}
@Test
public void testCfg1String2() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;O;N;F;C#c;f;n;o;p;w;x#S/xSnO,nSOO,wC;O/xFw,nNx;N/wSO,oS,e;F/c,pC,e;C/fCxF,n#S/x,n,w;O/x,n;N/w,o,e;F/c,p,e;C/f,n#S/$nx;O/$nx;N/x;F/$nwx;C/$nwx");
assertEquals("S;nSOO;nnSOOOO;nnwCOOOO;nnwnOOOO;nnwnnNxOOO;nnwnnwSOxOOO;ERROR", cfgLl1Parser.parse("nnwnnwco"));
}
@Test
public void testCfg1String3() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;O;N;F;C#c;f;n;o;p;w;x#S/xSnO,nSOO,wC;O/xFw,nNx;N/wSO,oS,e;F/c,pC,e;C/fCxF,n#S/x,n,w;O/x,n;N/w,o,e;F/c,p,e;C/f,n#S/$nx;O/$nx;N/x;F/$nwx;C/$nwx");
assertEquals("S;wC;wfCxF;wffCxFxF;wfffCxFxFxF;wfffnxFxFxF;wfffnxcxFxF;wfffnxcxxF;wfffnxcxxc", cfgLl1Parser.parse("wfffnxcxxc"));
}
@Test
public void testCfg1String4() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;O;N;F;C#c;f;n;o;p;w;x#S/xSnO,nSOO,wC;O/xFw,nNx;N/wSO,oS,e;F/c,pC,e;C/fCxF,n#S/x,n,w;O/x,n;N/w,o,e;F/c,p,e;C/f,n#S/$nx;O/$nx;N/x;F/$nwx;C/$nwx");
assertEquals("S;nSOO;nxSnOOO;nxxSnOnOOO;nxxwCnOnOOO;nxxwfCxFnOnOOO;nxxwfnxFnOnOOO;ERROR", cfgLl1Parser.parse("nxxwfnww"));
}
@Test
public void testCfg1String5() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;O;N;F;C#c;f;n;o;p;w;x#S/xSnO,nSOO,wC;O/xFw,nNx;N/wSO,oS,e;F/c,pC,e;C/fCxF,n#S/x,n,w;O/x,n;N/w,o,e;F/c,p,e;C/f,n#S/$nx;O/$nx;N/x;F/$nwx;C/$nwx");
assertEquals("S;nSOO;nnSOOOO;nnwCOOOO;nnwnOOOO;nnwnnNxOOO;nnwnnoSxOOO;nnwnnowCxOOO;nnwnnownxOOO;ERROR", cfgLl1Parser.parse("nnwnnown"));
}
@Test
public void testCfg2String1() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;Y;W;T;R#j;m;o;r;u;v;z#S/u,vS,oTuS,z;Y/vRo,jY,e;W/z,m,rSu,e;T/vR,rW,e;R/j,mSSY,z#S/u,v,o,z;Y/v,j,e;W/z,m,r,e;T/v,r,e;R/j,m,z#S/$ouvz;Y/ou;W/u;T/u;R/ou");
assertEquals("S;vS;vvS;vvvS;vvvvS;vvvvoTuS;vvvvorWuS;vvvvoruS;vvvvoruz", cfgLl1Parser.parse("vvvvoruz"));
}
@Test
public void testCfg2String2() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;Y;W;T;R#j;m;o;r;u;v;z#S/u,vS,oTuS,z;Y/vRo,jY,e;W/z,m,rSu,e;T/vR,rW,e;R/j,mSSY,z#S/u,v,o,z;Y/v,j,e;W/z,m,r,e;T/v,r,e;R/j,m,z#S/$ouvz;Y/ou;W/u;T/u;R/ou");
assertEquals("S;vS;voTuS;vorWuS;vorrSuuS;vorrvSuuS;vorrvvSuuS;vorrvvoTuSuuS;ERROR", cfgLl1Parser.parse("vorrvvo"));
}
@Test
public void testCfg2String3() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;Y;W;T;R#j;m;o;r;u;v;z#S/u,vS,oTuS,z;Y/vRo,jY,e;W/z,m,rSu,e;T/vR,rW,e;R/j,mSSY,z#S/u,v,o,z;Y/v,j,e;W/z,m,r,e;T/v,r,e;R/j,m,z#S/$ouvz;Y/ou;W/u;T/u;R/ou");
assertEquals("S;vS;voTuS;vorWuS;vormuS;vormuoTuS;vormuovRuS;ERROR", cfgLl1Parser.parse("vormuov"));
}
@Test
public void testCfg2String4() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;Y;W;T;R#j;m;o;r;u;v;z#S/u,vS,oTuS,z;Y/vRo,jY,e;W/z,m,rSu,e;T/vR,rW,e;R/j,mSSY,z#S/u,v,o,z;Y/v,j,e;W/z,m,r,e;T/v,r,e;R/j,m,z#S/$ouvz;Y/ou;W/u;T/u;R/ou");
assertEquals("S;oTuS;ovRuS;ovmSSYuS;ovmoTuSSYuS;ovmouSSYuS;ovmouoTuSSYuS;ERROR", cfgLl1Parser.parse("ovmouooj"));
}
@Test
public void testCfg2String5() {
CfgLl1Parser cfgLl1Parser= new CfgLl1Parser("S;Y;W;T;R#j;m;o;r;u;v;z#S/u,vS,oTuS,z;Y/vRo,jY,e;W/z,m,rSu,e;T/vR,rW,e;R/j,mSSY,z#S/u,v,o,z;Y/v,j,e;W/z,m,r,e;T/v,r,e;R/j,m,z#S/$ouvz;Y/ou;W/u;T/u;R/ou");
assertEquals("S;oTuS;ouS;ouvS;ouvoTuS;ouvovRuS;ouvovzuS;ouvovzuoTuS;ouvovzuouS;ouvovzuouz", cfgLl1Parser.parse("ouvovzuouz"));
}
}
Help me to Solve this Task
Dhruv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.