S
Shravani
hi,
i have to write a compiler for Monster language using C.example
programs of the language are:
EXAMPLE 1
/* Test Program - Module Structure */
module fact
export N2N;
export fact;
type N2N is -> int ret int throws;
val fact : N2N;
fact := fun(n) { if n=0 then return 1 else return n*fact(n-1); };
end
module main
import fact.N2N;
import fact.fact;
type TLF is -> int ret throws;
val x:int;
val main : TLF;
main := fun(n) { val result:int; result:= fact(n); print result; };
end
EXAMPLE 2
module Stack
export Stack;
type OverFlowException is * errMsg : str * size : int;
type UnderFlowException is * errMsg : str;
type Stack is * push : -> int ret throws OverflowException
* pop : -> ret throws UnderFlowException
* top : -> ret int throws UnderFlowException
* size : -> ret int throws
* isEmpty : -> ret int throws;
type IntArray is [] int;
val s : Stack;
val maxSize : int;
val ofe : OverFlowException;
maxSize := 100;
ofe := { "push invalid: stack is full", 0 };
s := obj {
val contents : IntArray;
val _size : int;
contents := [0, 0, 0, 0 ,0];
_size := 0;
size := fun() { return _size; };
isEmpty := fun() { return (size()) = 0; };
top := fun() { val ufe : UnderFlowException;
ufe.errMsg := "top invalid: stack is empty" ;
if isEmpty() then throw ufe else return contents[size()];
};
pop := fun() { val ufe : UnderFlowException;
ufe.errMsg := "top invalid: stack is empty" ;
if isEmpty() then throw ufe else _size := _size - 1 ;
};
push := fun(value) { if _size<maxSize then { _size := _size+1;
contents[_size] := value; }
else throw ofe;
};
};
end
module mymodule
import Stack.Stack;
import Stack.s;
type TopLevelFun is -> ret throws;
val stackTest:TopLevelFun;
stackTest:=fun() { val x:int; s.push(0); s.push(1); s.push(2);
s.pop(); s.push(3);
x:= s.top();
print x; s.pop();
y:= s.top();
print y;
};
end
What i have to do is to write a compiler for such programs.in the
first phase i need to write a scanner that produces the token stream
of the input program.that is:
* Scanner
Requirements Specification:
Input: Program File
Output: Token Stream
Side Effects: Comments and White spaces removed
Exceptions: Invalid tokens, Invalid comment
Interface Requirements:
void initializeScanner(char *filename);
Token nextToken(); // should scan and return the next token void
printTokenStream(FILE f);
here's the list of tokens:
/* Keywords */
1. TK_KEY_MOD "module"
2. TK_KEY_END "end"
3. TK_KEY_IMPORT "import"
4. TK_KEY_EXPORT "export"
5. TK_KEY_TYPE "type"
6. TK_KEY_IS "is"
7. TK_KEY_INT "int"
8. TK_KEY_FLOAT "float"
9. TK_KEY_STR "str"
10. TK_KEY_RET "ret"
11. TK_KEY_THROWS "throws"
12. TK_KEY_VAL "val"
13. TK_KEY_FUN "fun"
14. TK_KEY_IF "if"
15. TK_KEY_THEN "then"
16. TK_KEY_ELSE "else"
17. TK_KEY_PRINT "print"
18. TK_KEY_OBJ "obj"
19. TK_KEY_RETURN "return"
20. TK_KEY_THROW "throw"
21. TK_KEY_THIS "this"
/* Separators */
1. TK_SEMI ';'
2. TK_LPAR '('
3. TK_RPAR ')'
4. TK_COMMA ','
5. TK_COLON ':'
6. TK_LSQUA '['
7. TK_RSQUA ']'
8. TK_LBRACE '{'
9. TK_RBRACE '}'
/* Operators */
1. TK_ARROW '->'
2. TK_STAR '*'
3. TK_ASSIGN ':='
4. TK_DOT '.'
5. TK_MINUS '-'
6. TK_PLUS '+'
7. TK_DIV '/'
8. TK_HASH '#'
9. TK_LESS '<'
10. TK_GREAT '>'
11. TK_EQUAL '='
12. TK_AND '&'
13. TK_OR '|'
14. TK_NOT '!'
/* Other */
1. TK_ID (ALPHA | UNDER) (ALPHA | DIGIT | UNDER)*
2. TK_INTVAL DIGIT+
3. TK_FLOATVAL DIGIT+ '.' DIGIT+
4. TK_STRVAL '"' (NOQUOTE)* '"'
where ALPHA is any alphabetic character,
DIGIT is any digit character,
UNDER is the underscore character, and
NOQUOTE is any character except the double-quote.
could anyone please help me out in writing this scanner?i need to take
the source program as input and produce the token stream,consisting of
tokens of the form described above,as the output.
i have to write a compiler for Monster language using C.example
programs of the language are:
EXAMPLE 1
/* Test Program - Module Structure */
module fact
export N2N;
export fact;
type N2N is -> int ret int throws;
val fact : N2N;
fact := fun(n) { if n=0 then return 1 else return n*fact(n-1); };
end
module main
import fact.N2N;
import fact.fact;
type TLF is -> int ret throws;
val x:int;
val main : TLF;
main := fun(n) { val result:int; result:= fact(n); print result; };
end
EXAMPLE 2
module Stack
export Stack;
type OverFlowException is * errMsg : str * size : int;
type UnderFlowException is * errMsg : str;
type Stack is * push : -> int ret throws OverflowException
* pop : -> ret throws UnderFlowException
* top : -> ret int throws UnderFlowException
* size : -> ret int throws
* isEmpty : -> ret int throws;
type IntArray is [] int;
val s : Stack;
val maxSize : int;
val ofe : OverFlowException;
maxSize := 100;
ofe := { "push invalid: stack is full", 0 };
s := obj {
val contents : IntArray;
val _size : int;
contents := [0, 0, 0, 0 ,0];
_size := 0;
size := fun() { return _size; };
isEmpty := fun() { return (size()) = 0; };
top := fun() { val ufe : UnderFlowException;
ufe.errMsg := "top invalid: stack is empty" ;
if isEmpty() then throw ufe else return contents[size()];
};
pop := fun() { val ufe : UnderFlowException;
ufe.errMsg := "top invalid: stack is empty" ;
if isEmpty() then throw ufe else _size := _size - 1 ;
};
push := fun(value) { if _size<maxSize then { _size := _size+1;
contents[_size] := value; }
else throw ofe;
};
};
end
module mymodule
import Stack.Stack;
import Stack.s;
type TopLevelFun is -> ret throws;
val stackTest:TopLevelFun;
stackTest:=fun() { val x:int; s.push(0); s.push(1); s.push(2);
s.pop(); s.push(3);
x:= s.top();
print x; s.pop();
y:= s.top();
print y;
};
end
What i have to do is to write a compiler for such programs.in the
first phase i need to write a scanner that produces the token stream
of the input program.that is:
* Scanner
Requirements Specification:
Input: Program File
Output: Token Stream
Side Effects: Comments and White spaces removed
Exceptions: Invalid tokens, Invalid comment
Interface Requirements:
void initializeScanner(char *filename);
Token nextToken(); // should scan and return the next token void
printTokenStream(FILE f);
here's the list of tokens:
/* Keywords */
1. TK_KEY_MOD "module"
2. TK_KEY_END "end"
3. TK_KEY_IMPORT "import"
4. TK_KEY_EXPORT "export"
5. TK_KEY_TYPE "type"
6. TK_KEY_IS "is"
7. TK_KEY_INT "int"
8. TK_KEY_FLOAT "float"
9. TK_KEY_STR "str"
10. TK_KEY_RET "ret"
11. TK_KEY_THROWS "throws"
12. TK_KEY_VAL "val"
13. TK_KEY_FUN "fun"
14. TK_KEY_IF "if"
15. TK_KEY_THEN "then"
16. TK_KEY_ELSE "else"
17. TK_KEY_PRINT "print"
18. TK_KEY_OBJ "obj"
19. TK_KEY_RETURN "return"
20. TK_KEY_THROW "throw"
21. TK_KEY_THIS "this"
/* Separators */
1. TK_SEMI ';'
2. TK_LPAR '('
3. TK_RPAR ')'
4. TK_COMMA ','
5. TK_COLON ':'
6. TK_LSQUA '['
7. TK_RSQUA ']'
8. TK_LBRACE '{'
9. TK_RBRACE '}'
/* Operators */
1. TK_ARROW '->'
2. TK_STAR '*'
3. TK_ASSIGN ':='
4. TK_DOT '.'
5. TK_MINUS '-'
6. TK_PLUS '+'
7. TK_DIV '/'
8. TK_HASH '#'
9. TK_LESS '<'
10. TK_GREAT '>'
11. TK_EQUAL '='
12. TK_AND '&'
13. TK_OR '|'
14. TK_NOT '!'
/* Other */
1. TK_ID (ALPHA | UNDER) (ALPHA | DIGIT | UNDER)*
2. TK_INTVAL DIGIT+
3. TK_FLOATVAL DIGIT+ '.' DIGIT+
4. TK_STRVAL '"' (NOQUOTE)* '"'
where ALPHA is any alphabetic character,
DIGIT is any digit character,
UNDER is the underscore character, and
NOQUOTE is any character except the double-quote.
could anyone please help me out in writing this scanner?i need to take
the source program as input and produce the token stream,consisting of
tokens of the form described above,as the output.