Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > writing a compi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 9 Topic 24969 of 27679
Post > Topic >>

writing a compiler for Monster language using C language

by Shravani <gpaps87@[EMAIL PROTECTED] > Mar 15, 2008 at 11:51 PM

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

ex****t N2N;
ex****t 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

im****t fact.N2N;
im****t 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

ex****t 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

im****t Stack.Stack;
im****t 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_IM****T "im****t"
4. TK_KEY_EX****T "ex****t"
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.
 




 9 Posts in Topic:
writing a compiler for Monster language using C language
Shravani <gpaps87@[EMA  2008-03-15 23:51:26 
Re: writing a compiler for Monster language using C language
Morris Dovey <mrdovey@  2008-03-16 01:52:54 
Re: writing a compiler for Monster language using C language
"Malcolm McLean"  2008-03-16 09:18:53 
Re: writing a compiler for Monster language using C language
Keith Thompson <kst-u@  2008-03-16 02:20:06 
Re: writing a compiler for Monster language using C language
santosh <santosh.k83@[  2008-03-16 15:22:50 
Re: writing a compiler for Monster language using C language
"Bartc" <bc@  2008-03-16 13:05:28 
Re: writing a compiler for Monster language using C language
"Bartc" <bc@  2008-03-16 13:12:02 
Re: writing a compiler for Monster language using C language
Keith Thompson <kst-u@  2008-03-16 11:21:27 
Re: writing a compiler for Monster language using C language
"Bartc" <bc@  2008-03-16 21:36:15 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 21:23:23 CDT 2008.