Skip to content

AgrVanshika/Parser-for-Perl-like

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parser-for-Perl-like

Simple-Perl-Like Language

Language definition

Lexical Analyser

Usage Notation
a character x x
an escaped character \x
M followed by N MN
zero or more occurrences of M M*
one or more occurrences of M M+
zero or one occurrence of M M?
definition =
termination ;
alternation |
optional [ ... ]
repetition { ... }
grouping ( ... )
terminal string ' ... '
exception -
  • Identifiers (IDENT)

    • IDENT := [ Letter _ ] {( Letter | Digit | _ )}
    • Letter := [ a-z A-Z ]
    • Digit := [0-9]
  • The variables are either numeric or string scalar variables. Numeric variables start by "$" while a string variables start by "@". Both are followed by an IDENT.

    • NIDENT := $ IDENT
    • SIDENT := @ IDENT
  • Integer constants (ICONST)

    • ICONST := [0-9]+
  • Real constants (RCONST)

    • RCONST := ([0-9]+).([0-9]*)
  • String constants (SCONST)

    • SCONST := '[anything]+'
  • Reserved words: writeln, if, and else

    • Tokens, respectively: WRITELN, IF, ELSE
  • Terminals: semicolon, comma, left parenthesis, right parenthesis, left braces, and right braces

    • Tokens, respectively: SEMICOL, COMMA, LPAREN, RPAREN, LBRACES, and RBRACES
  • Comment is defined by anything following “#” to the end of line and does not have a token

  • Operators: +, -, *, /, ^, =, ==, >, <, . (dot), ** (repeat), -eq (string equality), -lt (string less than), and -gt (string greater than)

    • Tokens, respectively: PLUS, MINUS, MULT, DIV, EXPONENT, ASSOP, NEQ, NGTHAN, NLTHAN, CAT, SREPEAT, SEQ, SLTHAN, and SGTHAN
  • Error (ERR)

  • End of file (DONE)

Recursive-Desecent Parser

  • Definition (using recursion):
1. Prog ::= StmtList
2. StmtList ::= Stmt ;{ Stmt; }
3. Stmt ::= AssignStme | WriteLnStmt | IfStmt
4. WriteLnStmt ::= writeln (ExprList)
5. IfStmt ::= if (Expr) ‘{‘ StmtList ‘}’ [ else ‘{‘ StmtList ‘}’ ]
6. AssignStmt ::= Var = Expr
7. Var ::= NIDENT | SIDENT
8. ExprList ::= Expr { , Expr }
9. Expr ::= RelExpr [(-eq|==) RelExpr ]
10. RelExpr ::= AddExpr [ ( -lt | -gt | < | > ) AddExpr ]
11. AddExpr :: MultExpr { ( + | - | .) MultExpr }
12. MultExpr ::= ExponExpr { ( * | / | **) ExponExpr }
13. ExponExpr ::= UnaryExpr { ^ UnaryExpr }
14. UnaryExpr ::= [( - | + )] PrimaryExpr
15. Primary ::= IDENT | SIDENT | NIDENT | ICONST | RCONST | SCONST | (Expr)
  • Table of Operators Precedence Levels:
Precedence Operator Description Associativity
1 Unary + , - Unary plus and minus Right to Left
2 ^ Exponent Right to Left
3 *, /, ** Multiplication, Division, and String repetition Left to Right
4 +, -, . (Dot) Addition, Subtraction, and String concatenation Left to Right
5 <, >, -gt, -lt - Numeric Relational
- String Relational
no cascading
6 ==, -eq - Numeric Equality
- String Equality
no cascading

Interpreter

  • Implement Value class member functions and overloaded operators from val.h

  • The interpreter provides:

    • Perform the syntax analysis of the input source code, statement by statement, then execute the statement if there is no error.
    • Build the information of the variables types in the symbol table for all the given defined variables.
    • Next, evaluate the expressions and determine the corresponding values and types.

How its built:

  • Coding language: C++
  • Libraries/Frameworks: string, queue, map, iomanip, stdexcept, cmath, sstream, and iostream

How to run:

  • Clone this repo to your local IDE (VSCode/CLion).
  • Run prog3.cpp by command line (including the testcase file name).

About

Parser for a Perl-like-language given the EBNF notation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages