4. Sample Output
Using the program nfact.mns two expamples of the output produced by the
parser are shown. Feel free to make additions that you deem necessary.
Program nfact.mns
[EFS]>cat nfact.mns
/**************
recursive N factorial. Used
for testing recursion. Also
used to make sure that the
answer from recursive n! is the
same as iterative n!.
**************/
/* recursively call n!*/
int nfact(int n)
{
if( n == 0 )
{
return 1;
}
else
{
return(n*nfact(n-1));
}
return 0;
}
void main(void)
{
int n;
n = input();
n = nfact(n);
output(n);
}
[EFS]>
Example #1
[EFS]>cminus nfact.mns
CMINUS COMPILATION: nfact.mns
Syntax tree:
INT Func: nfact
INT PARAM: n
Compound Block
If
Op: ==
Id: n
Const: 0
Then
Compound Block
Return
Const: 1
else
Compound Block
Return
Op: *
Id: n
CALL: nfact
Op: -
Id: n
Const: 1
endif
Return
Const: 0
VOID Func: main
VOID PARAM: (null)
Compound Block
INT VAR: n
Assign:
Id: n
Input()
Assign:
Id: n
CALL: nfact
Id: n
Output:
Id: n
[EFS]>
Example #2
[EFS]>c- nfact.mns
*********C- COMPILATION: nfact.mns*********
and creating syntax tree...
Syntax tree:
Function; id:nfact return type:int
Function parameter list:
Integer Declaration; id:n
CompStmtK:
If
Op: ==
Integer Variable; id:n
Const: 0
Then
CompStmtK:
ReturnK:
Const: 1
Else
CompStmtK:
ReturnK:
Op: *
Integer Variable; id:n
CallK; name: nfact
Call argument list:
Op: -
Integer Variable; id:n
Const: 1
Endif
ReturnK:
Const: 0
Function; id:main return type:void
Function Parameter list:Void
CompStmtK:
Integer Declaration; id:n
Assignment:
Integer Variable; id:n
CallK; name: input
Void argument list.
Assignment:
Integer Variable; id:n
CallK; name: nfact
Call argument list:
Integer Variable; id:n
CallK; name: output
Call argument list:
Integer Variable; id:n
[EFS]>
|