1. Homepage
  2. Programming
  3. CSE340 Principles of Programming Languages - Project 2: Parsing

CSE340 Principles of Programming Languages - Project 2: Parsing

Engage in a Conversation
ASUCSE340Principles of Programming LanguagesC++Recursive Descent Parser

CSE340 Project 2: Parsing CourseNana.COM

The goal of this project is to give you experience in writing a top-down recursive descent parser and to get introduced to the basics of symbol tables for nested scopes. CourseNana.COM

We begin by introducing the grammar of our language. Then we will discuss the semantics of our language that involves lexical scoping rules and name resolution. Finally, we will go over a few examples and formalize the expected output. CourseNana.COM

NOTE: This project is significantly more involved than the first project. You should start on it immediately. CourseNana.COM


1. Lexical Specification
Here is the list of tokens that your lexical analyzer needs to support: CourseNana.COM

PUBLIC = “public” PRIVATE = “private” EQUAL = “=”
COLON = “:” CourseNana.COM

COMMA = “,”
SEMICOLON = “;”
LBRACE = “{”
RBRACE = “}” ID=letter(letter+ digit)* CourseNana.COM

Comments and Space CourseNana.COM

In addition to these tokens, our input programs might have comments that should be ignored by the lexical analyzer. A comment starts with // and continues until a newline character is encountered. The regular expressions for comments is: // (any)* \n in which any is defined to be any character except \n . Also, like in the first project, your lexical analyzer should skip space between tokens. CourseNana.COM

2. Grammar CourseNana.COM

Here is the grammar for our input language: CourseNana.COM

program
global_vars
global_vars
var_list
var_list
scope
public_vars
public_vars
private_vars
private_vars
stmt_list
stmt_list
stmt

stmt CourseNana.COM

® global_vars scope ® e
®
var_list SEMICOLON ® ID CourseNana.COM

® ID COMMA var_list
® ID LBRACE public_vars private_vars stmt_list RBRACE ® e
®
PUBLIC COLON var_list SEMICOLON
® e
®
PRIVATE COLON var_list SEMICOLON
® stmt
® stmt stmt_list
® ID EQUAL ID SEMICOLON
® scope CourseNana.COM

Here is an example input program with comments: CourseNana.COM

a, b, c; CourseNana.COM

test { CourseNana.COM

public: CourseNana.COM

a, b, hello; CourseNana.COM

private: CourseNana.COM

x, y; CourseNana.COM

      a = b;
      hello = c;
      y = r;
      nested {

// These are global variables CourseNana.COM

// These are public variables of scope test // These are private variables of scope test // the body of test starts with this line CourseNana.COM

// this is a nested scope
// which does not have private variables
CourseNana.COM

have lines that only contain comments like this CourseNana.COM

public: CourseNana.COM

b; a = b; CourseNana.COM

x = hello;
c = y;
// we can also

Note that our grammar does not recognize comments, so our parser would not know anything about comments, but our lexical analyzer would deal with comments. This is similar to handling of spaces by the lexer, the lexer skips the spaces. In a similar fashion, your lexer should skip CourseNana.COM

comments.
We highlight some of the syntactical elements of the language:
CourseNana.COM

Global variables are optional
The scopes have optional public and private variables
Every scope has a body which is a list of statements
A statement can be either a simple assignment or another scope (a nested scope)
CourseNana.COM

3. Scoping and Resolving References CourseNana.COM

Here are the scoping rules for our language: CourseNana.COM

The public variables of a scope are accessible to its nested scopes CourseNana.COM

The private variables of a scope are not accessible to its nested scopes CourseNana.COM

Lexical scoping rules are used to resolve name references Global variables are accessible to all scopes CourseNana.COM

Every reference to a variable is resolved to a specific declaration by specifying the variable's defining scope. We will use the following notation to specify declarations: CourseNana.COM

  • If variable a is declared in the global variables list, we use ::a to refer to it CourseNana.COM

  • If variable a is declared in scope b, we use b.a to refer to it CourseNana.COM

    And if reference to name a cannot be resolved, we denote that by ?.a CourseNana.COM

Here is the example program from the previous section, with all name references resolved (look at the comments): CourseNana.COM

a, b, c; CourseNana.COM

test { CourseNana.COM

public: CourseNana.COM

a, b, hello; CourseNana.COM

private: CourseNana.COM

x, y; CourseNana.COM

      a = b;
      hello = c;
      y = r;

nested { CourseNana.COM

public: CourseNana.COM

b; a = b; CourseNana.COM

            x = hello;
            c = y;
   // test.a = test.b
  // test.hello = ::c
 // test.y = ?.r
// test.a = nested.b
// ?.x = test.hello
// ::c = ?.y

4. Examples CourseNana.COM

The simplest possible program would be: CourseNana.COM

Let's add a global variable: CourseNana.COM

main {
a = a;
// ?.a = ?.a CourseNana.COM

} CourseNana.COM

a; main { CourseNana.COM

a = a; // ::a = ::a } CourseNana.COM

Now, let's add a public variable a: CourseNana.COM

a; main { CourseNana.COM

public: CourseNana.COM

a; a = a; CourseNana.COM

// main.a = main.a

Or a private a: CourseNana.COM

a; main { CourseNana.COM

private: CourseNana.COM

a; a = a; CourseNana.COM

// main.a = main.a

Now, let's see a simple example with nested scopes: CourseNana.COM

a, b; main { CourseNana.COM

nested {
a=b;
//::a=::b CourseNana.COM

} } CourseNana.COM

If we add a private variable in main: CourseNana.COM

a, b; main { CourseNana.COM

private: CourseNana.COM

a; nested { CourseNana.COM

a=b; //::a=::b } CourseNana.COM

} CourseNana.COM

And a public b: CourseNana.COM

a, b; main { CourseNana.COM

public: b; CourseNana.COM

      private:
            a;
      nested {

a=b; //::a=main.b } CourseNana.COM

} CourseNana.COM

You can find more examples by looking at the test cases and their expected outputs. CourseNana.COM

5. Expected Output CourseNana.COM

There are two cases:
In case the input does not follow the grammar, the expected output is:
CourseNana.COM

NOTE: no extra information is needed here! Also, notice that we need the exact message and it's case-sensitive. CourseNana.COM

In case the input follows the grammar: CourseNana.COM

For every assignment statement in the input program in order of their appearance in the program, output the following information: CourseNana.COM

  • The resolved left-hand-side of the assignment CourseNana.COM

  • The resolved right-hand-side of the assignment CourseNana.COM

    in the following format: CourseNana.COM

    NOTE: You can assume that scopes have unique names and variable names in a single scope (public and private) are not repeated. CourseNana.COM

Syntax Error CourseNana.COM

resolved_lhs = resolved_rhs CourseNana.COM

For example, given the following input program: CourseNana.COM

a, b, c; CourseNana.COM

test { CourseNana.COM

public: CourseNana.COM

a, b, hello; CourseNana.COM

private: CourseNana.COM

x, y; CourseNana.COM

      a = b;
      hello = c;
      y = r;
      nested {

public: CourseNana.COM

b; a = b; CourseNana.COM

x = hello; CourseNana.COM

c = y; } CourseNana.COM

} CourseNana.COM

The expected output is: CourseNana.COM

6. Implementation CourseNana.COM

Start by modifying the lexical analyzer from previous project to make it recognize the tokens required for parsing this grammar. It should also be able to handle comments (skip them like spaces).
NOTE: make sure you remove the tokens that are not used in this grammar from your lexer, otherwise you might not be able to pass all test cases. Your TokenType type declaration should look like this: CourseNana.COM

test.a = test.b
test.hello = ::c
test.y = ?.r
test.a = nested.b
?.x = test.hello
::c = ?.y

typedef enum { END_OF_FILE = 0, PUBLIC, PRIVATE, CourseNana.COM

EQUAL, COLON, COMMA, SEMICOLON, CourseNana.COM

LBRACE, RBRACE, ID, ERROR } TokenType CourseNana.COM

Next, write a parser for the given grammar. You would need one function per each non-terminal of the grammar to handle parsing of that non-terminal. I suggest you use the following signature for these functions: CourseNana.COM

        void parse_X()

Where X would be replaced by the target non-terminal. The lexical analyzer object needs to be accessible to these functions so that they can use the lexer to get and unget tokens. These functions can be member functions of a class, and the lexer object can be a member variable of that class. CourseNana.COM

You also need a syntax_error function that prints the proper message and terminates the program: CourseNana.COM

Test your parser thoroughly. Make sure it can detect any syntactical errors. CourseNana.COM

Next, write a symbol table that stores information about scopes and variables. You would also need to store assignments in a list to be accessed after parsing is finished. You need to think about how to organize all this information in a way that is useful for producing the required output. CourseNana.COM

Write a function that resolves the left-hand- side and right-hand-side of all assignments and produces the required output. Call this function in your main() function after successfully parsing the input. CourseNana.COM

NOTE: you might need more time to finish the last step compared to previous steps. 7. Requirements CourseNana.COM

Here are the requirements of this project:
You should submit
all your project files (source code [.cc] and headers[.h]) on CourseNana.COM

void syntax_error() { CourseNana.COM

cout << “Syntax Error\n”; CourseNana.COM

exit(1); } CourseNana.COM

Gradescope. Do not zip them. CourseNana.COM

You should use C/C++, no other programming languages are allowed.
Besides the provided test cases, you need to design test cases on your own to rigorously test your implementation.
CourseNana.COM

You should test your code on Ubuntu Linux 19.04 or greater with gcc 7.5.0 or higher. CourseNana.COM

CourseNana.COM

You cannot use library methods for parsing or regular expression (regex) matching in projects. You will be implementing them yourself. If you have doubts about using a library method, please check it with the instructor or TA beforehand. CourseNana.COM

You can write helper methods or have extra files, but they should have been written by you. CourseNana.COM

8. Evaluation CourseNana.COM

The submissions are evaluated based on the automated test cases on the Gradescope. Gradescope test cases are hidden to students. Your grade will be proportional to the number of test cases passing. You have to thoroughly test your program to ensure it pass all the possible test cases. It is not guaranteed that your code will pass the Gradescope test cases if it passes the published test cases. As a result, in addition to the provided test cases, you must design your own test cases to rigorously evaluate your implementation. If your code does not compile on the submission website, you will not receive any points. On Gradescope, when you get the results back, ignore the “Test err” case, it is not counted toward the grade. CourseNana.COM

The parsing test cases contain cases that are syntactically correct and cases that have syntax errors. If a syntax test case has no syntax error, your program passes the test case if the output is not Syntax Error . If a syntax test case has syntax error, your program passes the test case if the output is Syntax Error . CourseNana.COM

Note that if your program prints the syntax error message independently of the input, for example: CourseNana.COM

It will pass some of the test cases, but you will not receive any points. CourseNana.COM

You can access the Gradescope through the left side bar in canvas. You have already been enrolled in the grade scope class, and using the left side bar in canvas you will automatically get into the Gradescope course. CourseNana.COM

int main() { CourseNana.COM

cout << “Syntax Error\n”; CourseNana.COM

return 0; }  CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
ASU代写,CSE340代写,Principles of Programming Languages代写,C++代写,Recursive Descent Parser代写,ASU代编,CSE340代编,Principles of Programming Languages代编,C++代编,Recursive Descent Parser代编,ASU代考,CSE340代考,Principles of Programming Languages代考,C++代考,Recursive Descent Parser代考,ASUhelp,CSE340help,Principles of Programming Languageshelp,C++help,Recursive Descent Parserhelp,ASU作业代写,CSE340作业代写,Principles of Programming Languages作业代写,C++作业代写,Recursive Descent Parser作业代写,ASU编程代写,CSE340编程代写,Principles of Programming Languages编程代写,C++编程代写,Recursive Descent Parser编程代写,ASUprogramming help,CSE340programming help,Principles of Programming Languagesprogramming help,C++programming help,Recursive Descent Parserprogramming help,ASUassignment help,CSE340assignment help,Principles of Programming Languagesassignment help,C++assignment help,Recursive Descent Parserassignment help,ASUsolution,CSE340solution,Principles of Programming Languagessolution,C++solution,Recursive Descent Parsersolution,