Assignment 2

Assigned Wednesday, September 17, due Wednesday, September 24


To Do

  1. You will be given a Java application for creating context free grammars.  This application stores context free grammars as XML files.
  2. Write a program to create the First sets for all nonterminals and terminals in a context free grammar provided in this XML format.
  3. Write a program to create the Follow sets for all nonterminals in a context free grammar provided in this XML format.
  4. Use the context free grammar application to enter the three grammars for expressions given in class.  For each grammar, run your First and Follow algorithms.  Print the results.

 

Sample XML file

<?xml version="1.0"?>

<!DOCTYPE GRAMMAR [
<!ELEMENT GRAMMAR (TITLE, START, PRODUCTION+)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT START (#PCDATA)> 
<!ELEMENT PRODUCTION ( LEFTSIDE, RIGHTSIDE)>
<!ELEMENT LEFTSIDE (#PCDATA)>
<!ELEMENT RIGHTSIDE (SYMBOL)+ >
<!ELEMENT SYMBOL (#PCDATA)>
<!ATTLIST SYMBOL Term (terminal|nonterminal) #REQUIRED>
]>

<GRAMMAR>
<TITLE>Arithmetic Expression #1</TITLE>
<START>Expression</START>
<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
		<SYMBOL Term="terminal">+</SYMBOL>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>
<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
		<SYMBOL Term="terminal">-</SYMBOL>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>

<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
		<SYMBOL Term="terminal">*</SYMBOL>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>

<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
		<SYMBOL Term="terminal">/</SYMBOL>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>
<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="terminal">(</SYMBOL>
		<SYMBOL Term="nonterminal">Expression</SYMBOL>
		<SYMBOL Term="terminal">)</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>

<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="terminal">iden</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>

<PRODUCTION>
	<LEFTSIDE>Expression</LEFTSIDE>
	<RIGHTSIDE>
		<SYMBOL Term="terminal">int_lit</SYMBOL>
	</RIGHTSIDE>
</PRODUCTION>

</GRAMMAR>