Programming Languages - Study Notes for Assignment 3 | CS 401, Assignments of Programming Languages

Material Type: Assignment; Class: Programming Languages; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Spring 2005;

Typology: Assignments

Pre 2010

Uploaded on 04/12/2010

koofers-user-8ea
koofers-user-8ea 🇺🇸

7 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 405/505 ASSIGNMENT #3
Due Thursday, January 20, 2005
Consider the following extended BNF grammar for a subset of the Haskell functional programming language,
called MicroHaskell.
program ::= dec-list
exp ::= Boolean-exp
|let dec-list in exp
|if ( Boolean-exp ) then ( exp ) else ( exp )
atomic-exp ::= integer |(exp )|identifier {parameter-exp}
parameter-exp ::= integer |(exp )|identifier
dec-list ::= dec {dec }
dec ::= identifier = exp |identifier argument-list = exp
argument-list ::= identifier {identifier}
Boolean-exp ::= relational-exp {Boolean-operator relational-exp}
Boolean-operator ::= && | ||
relational-exp ::= list-exp [relational-operator list-exp]
relational-operator ::= == |/=|<|<=|>|>=
list-exp ::= primary-exp : list-exp |tail atomic-exp |[ ] |primary-exp
primary-exp ::= term {adding-operator term}
adding-operator ::= +|
term ::= factor {multiplying-operator factor}
multiplying-operator ::= *|/
factor ::= [head] atomic-exp
Syntactic and Semantic Conventions
The keywords and the token symbols in MicroHaskell are in bold. Note that all operators are left associative
except for : which is right associative. Assume that an identifier can only contain letters (only alphabetic
characters), digits, and underscores ( ) with the restrictions that it must begin with a letter, cannot end with
an underscore and cannot have two consecutive underscores. For example, give 2 Joe, tell me and A45Asm3
are valid identifiers, but 6gh, two bad, and no end are not. integer is an unsigned integer. Keywords are
reserved and hence cannot be used as identifiers. Comments are indicated by being enclosed in {-and -},
possibly spanning multiple lines.
1. Determine the set of tokens which a lexical analyzer would need to recognize.
2. Design and implement a lexical analyzer procedure to read a source program in the above language
and print the next token in the input stream. If the token detected is a valueless token, such as a
keyword, then it is sufficient to print only the keyword. If it has a value, then both the token type and
lexeme should be printed.
3. You will be given several MicroHaskell programs with which to test your lexical analyzer. These will
be placed on the class WWW page and will be of the form Test1.hs, Test2.hs, etc.
Suggestions:
1. Construct a token class to represent the token data structure, including a method to print a token.
2. Use the Java JLex tool to automatically construct a lexical analyzer in Java from a set of regular
expressions specifying tokens. This can interface with your token class to return a token to the main
program which then prints the token.

Partial preview of the text

Download Programming Languages - Study Notes for Assignment 3 | CS 401 and more Assignments Programming Languages in PDF only on Docsity!

CS 405/505 ASSIGNMENT

Due Thursday, January 20, 2005

Consider the following extended BNF grammar for a subset of the Haskell functional programming language, called MicroHaskell.

program ::= dec-list exp ::= Boolean-exp | let dec-list in exp | if ( Boolean-exp ) then ( exp ) else ( exp ) atomic-exp ::= integer | ( exp ) | identifier {parameter-exp} parameter-exp ::= integer | ( exp ) | identifier dec-list ::= dec { dec } dec ::= identifier = exp | identifier argument-list = exp argument-list ::= identifier {identifier} Boolean-exp ::= relational-exp {Boolean-operator relational-exp} Boolean-operator ::= && | || relational-exp ::= list-exp [relational-operator list-exp] relational-operator ::= == | / = | < | <= | > | >= list-exp ::= primary-exp : list-exp | tail atomic-exp | [ ] | primary-exp primary-exp ::= term {adding-operator term} adding-operator ::= + | − term ::= factor {multiplying-operator factor} multiplying-operator ::= * | / factor ::= [head] atomic-exp

Syntactic and Semantic Conventions

The keywords and the token symbols in MicroHaskell are in bold. Note that all operators are left associative except for : which is right associative. Assume that an identifier can only contain letters (only alphabetic characters), digits, and underscores ( ) with the restrictions that it must begin with a letter, cannot end with an underscore and cannot have two consecutive underscores. For example, give 2 Joe, tell me and A45Asm are valid identifiers, but 6gh, two bad, and no end are not. integer is an unsigned integer. Keywords are reserved and hence cannot be used as identifiers. Comments are indicated by being enclosed in {- and - }, possibly spanning multiple lines.

  1. Determine the set of tokens which a lexical analyzer would need to recognize.
  2. Design and implement a lexical analyzer procedure to read a source program in the above language and print the next token in the input stream. If the token detected is a valueless token, such as a keyword, then it is sufficient to print only the keyword. If it has a value, then both the token type and lexeme should be printed.
  3. You will be given several MicroHaskell programs with which to test your lexical analyzer. These will be placed on the class WWW page and will be of the form Test1.hs, Test2.hs, etc.

Suggestions:

  1. Construct a token class to represent the token data structure, including a method to print a token.
  2. Use the Java JLex tool to automatically construct a lexical analyzer in Java from a set of regular expressions specifying tokens. This can interface with your token class to return a token to the main program which then prints the token.