Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Languages - Grammar Write your own grammar for a programming languag

ID: 3833512 • Letter: P

Question

Programming Languages - Grammar

Write your own grammar for a programming language (The set of rules that tell you whether a sentence is correctly structured is called a grammar). You will need to create a context-free grammar for your language. This grammar will need to be able to be recognized via recursive descent parsing. A recognizer is a program which says whether the expressions (sentences) in your source code are syntactically legal. A recursive descent parser is composed of a set of parsing functions, each of which derives from a rule in the grammar. Not all grammars are suitable for recursive descent parsing. Anything that is your own grammar that will work as a programming language is acceptable for an answer to this question as long as it is complete.

An Example using plain English:

sentence : nounPhrase verbPhrase PERIOD

nounPhrase : ARTICLE Adjective NOUN

verbPhrase : VERB nounPhrase

then with further modification the language can use adjectives and finally be:

sentence : nounPhrase verbPhrase PERIOD

nounPhrase : optArticle optAdjList NOUN

verbPhrase : VERB nounPhrase

optArticle : ARTICLE | *empty*

optAdjList : adjList | *empty*

adjList : ADJECTIVE | ADJECTIVE adjLis

KUDOS the person that can do this correctly and help me with this.

Explanation / Answer

import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; import java.io.StringReader; import semanticengine.Description; import edu.stanford.nlp.objectbank.TokenizerFactory; import edu.stanford.nlp.process.CoreLabelTokenFactory; import edu.stanford.nlp.process.DocumentPreprocessor; import edu.stanford.nlp.process.PTBTokenizer; import edu.stanford.nlp.ling.CoreLabel; import edu.stanford.nlp.ling.HasWord; import edu.stanford.nlp.ling.TaggedWord; import edu.stanford.nlp.trees.*; import edu.stanford.nlp.parser.lexparser.LexicalizedParser; public class EnglishParser { public static LexicalizedParser lp = null; public static void main(String[] args) { EnglishParser MC=new EnglishParser(); Scanner sc=new Scanner(System.in); String s=""; while(s!="end") { s=sc.nextLine(); ArrayList AT=MC.Parse(s); Description obj= new Description(AT ); System.out.println (AT); } } public static void demoDP(LexicalizedParser lp, String filename) { // This option shows loading and sentence-segment and tokenizing // a file using DocumentPreprocessor TreebankLanguagePack tlp = new PennTreebankLanguagePack(); GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(); // You could also create a tokenier here (as below) and pass it // to DocumentPreprocessor for (List sentence : new DocumentPreprocessor(filename)) { Tree parse = lp.apply(sentence); parse.pennPrint(); System.out.println(); GrammaticalStructure gs = gsf.newGrammaticalStructure(parse); Collection tdl = gs.typedDependenciesCCprocessed(true); System.out.println(tdl); System.out.println(); } } //Method for Pos taging.(POS) tagger that assigns its class //(verb, adjective, ...) to each word of the sentence, //para@ english is the argument to be tagged public ArrayList Parse(String English) { String[] sent =English.split(" ");// { "This", "is", "an", "easy", "sentence", "." }; List rawWords = new ArrayList(); for (String word : sent) { CoreLabel l = new CoreLabel(); l.setWord(word); rawWords.add(l); } Tree parse = lp.apply(rawWords); return parse.taggedYield(); } public EnglishParser() { lp = new LexicalizedParser("grammar/englishPCFG.ser.gz"); } // static methods only } // return pattern of the sentence public String getPattern(ArrayList Sen) { Iterator its = Sen.iterator(); while (its.hasNext()) { TaggedWord obj = its.next(); if ((obj.tag().equals("VBZ")) || (obj.tag().equals("VBP"))) { if (its.hasNext()) { TaggedWord obj2 = its.next(); if (obj2.tag().equals("VBG")) { if (its.hasNext()) { TaggedWord obj3 = its.next(); if ((obj3.tag().equals("VBN"))) { return "PRESENT_CONT_PASS"; } } return "PRESENT_CONT"; // Present Continues } else if ((obj2.tag().equals("VBN"))) { return "PRESENT_PASS"; } return "PRESENT_SIMP"; } else { return "PRESENT_SIMP"; } } else if (obj.tag().equals("VBD")) { if (its.hasNext()) { TaggedWord obj2 = its.next(); if (obj2.tag().equals("VBG")) { if (its.hasNext()) { TaggedWord obj3 = its.next(); if ((obj3.tag().equals("VBN"))) { return "PATT_CONT_PASS"; } } return "PAST_CONT"; } else if ((obj2.tag().equals("VBN"))) { return "PAST_PASS"; } return "PAST_SIMP"; } else { return "PAST_SIMP"; } } else if (obj.tag().equals("VB")) { if (its.hasNext()) { TaggedWord obj2 = its.next(); if (obj2.tag().equals("VBG")) { return "FUT_CONT"; } else if ((obj2.tag().equals("VBN"))) { return "FUT_CONT"; } } else { return "FUT_SIMP"; } } } return "NO_PATTERN"; }