⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pro.g

📁 使用antlr实现c和pascal的转换
💻 G
字号:
grammar PRO;
options {
    backtrack=true;
    k=9;
}
@header {
package test;
import java.util.HashMap;

}

@lexer::header {package test;}

@members {
/** Map variable name to Integer object holding value */
HashMap memory = new HashMap();
static int tag = 0;

private static String newtemp()
{
	return new String("t"+Integer.toString(tag++));
}
private static void emit(String s)
{
	System.out.print(s);
}
}

prog:    program;

program	:PROGRAM SEPARATOR* ID '(' identifier_list ')' SEPARATOR*
         declarations SEPARATOR*
         subprogram_declarations SEPARATOR*	{emit("void main(){\n");}
         compound_statement SEPARATOR*		{emit("}");}
         '.'
	;

declarations	returns [String place]
	:(){$place="";} (VAR SEPARATOR* identifier_list SEPARATOR* ':' SEPARATOR* type SEPARATOR* ';'{$place+=$type.place+" "+$identifier_list.place+";\n";})*	
	;
identifier_list returns [String place]
	:(e=ID{$place=$e.text;}) (SEPARATOR* ',' SEPARATOR* f=ID{$place+=","+$f.text;})*	
	;	
type    returns [String place]
	:standard_type	{$place=$standard_type.place;}
	|ARRAY '[' e=NUM '..' f=NUM ']' 'OF' standard_type 	{$place=$standard_type.place+"["+Integer.toString(Integer.parseInt($e.text)-Integer.parseInt($f.text)+1)+"]";}
	;
standard_type returns [String place]
	:INTEGER{$place="int";}
	|REAL	{$place="float";}
	;
subprogram_declarations
	:() (SEPARATOR* subprogram__declaration SEPARATOR* ';' SEPARATOR*)*	
	;
subprogram__declaration
	:subprogram_head SEPARATOR* {emit($subprogram_head.place+"\n");} declarations SEPARATOR* {emit($declarations.place+"\n");} compound_statement SEPARATOR*	
	;	
subprogram_head	returns [String place]
	:FUNCTION SEPARATOR* ID SEPARATOR* arguments SEPARATOR* ':' SEPARATOR* standard_type SEPARATOR* ';' SEPARATOR*	{$place=$standard_type.place+" "+$ID.text+$arguments.place;}
	|PROCEDURE SEPARATOR* ID SEPARATOR* arguments SEPARATOR* ';' SEPARATOR*		{$place="void "+$ID.text+$arguments.place;}
	;
arguments  returns [String place]
	:'(' SEPARATOR* parameter_list SEPARATOR* ')'{$place="("+$parameter_list.place+")";}
	|	{$place="()";}
	;
parameter_list returns [String place]
	:(e=identifier_list SEPARATOR* ':' SEPARATOR* g=type{$place=$g.place+" "+$e.place;}) (SEPARATOR* ';' SEPARATOR* f=identifier_list SEPARATOR* ':' SEPARATOR* h=type{$place+=","+$h.place+" "+$f.place;})*
	;


compound_statement
	:BEGIN SEPARATOR*{emit("{\n");}
	optional_statements (|';')SEPARATOR* 
	END SEPARATOR* {emit("}\n");}
	;
optional_statements
	:statement_list
	|	{emit(";\n");}
	;
statement_list
	:statement (';' SEPARATOR* statement)*	
	;			
statement
	:compound_statement
	|variable SEPARATOR* ASSIGNOP SEPARATOR* expression			{emit($variable.place);
							 emit("=");
							 emit($expression.place);
							 emit(";\n");
							}
	|procedure_statement				{emit($procedure_statement.place);
							 emit(";\n");}
	|IF SEPARATOR* expression SEPARATOR* THEN SEPARATOR* {emit("if(");emit($expression.place);emit(")\n");}statement SEPARATOR* ELSE SEPARATOR* {emit("else\n");} statement 	
							 
							 
							
	|WHILE SEPARATOR* expression SEPARATOR* DO SEPARATOR*{emit("while(");emit($expression.place);emit(")\n");emit("do\n");}statement 			
	;	
variable	returns [String place]	
	:ID '[' SEPARATOR* expression SEPARATOR* ']'	{$place=$ID.text+"["+$expression.place+"]";}
	|ID			{$place=$ID.text;}
	;
procedure_statement	returns [String place]
	:ID '(' SEPARATOR* expression_list SEPARATOR* ')'{$place=$ID.text+"("+$expression_list.place+")";}
	|ID			   {$place=$ID.text+"()";}				
	;	
expression_list  returns [String place]
	:	(e=expression			{$place=$e.place;}) (','f=expression	{$place=$place+","+$f.place;})*
	; 
expression	returns [String place]
	:	e=simple_expression SEPARATOR* relop SEPARATOR* f=simple_expression		
{$place=$e.place+$relop.place+$f.place;}
	|	simple_expression					{$place=$simple_expression.place;}
	;
simple_expression	returns [String place]
	:	(e=term		{$place=$e.place;} | sign SEPARATOR* e=term	{$place=$sign.place+$e.place;}) (SEPARATOR* addop SEPARATOR* f=term{$place=$place+$addop.place+$f.place;})*
	;
term    returns [String place]
	:	(e=factor				{$place=$e.place;}) (SEPARATOR* mulop SEPARATOR* f=factor		{$place=$place+$mulop.place+$f.place;})*
	;
factor  returns [String place]
	:	'(' expression ')'		{$place="("+$expression.place+")";}
	|	ID '(' expression_list ')' SEPARATOR*	{$place=$ID.text+"("+$expression_list.place+")";}
	|	ID				{$place=$ID.text;}
	|	NUM				{$place=$NUM.text;}
	|	NOT SEPARATOR* factor			{$place="!("+$factor.place+")";};
relop   returns [String place]
	:	EQUAL             {$place=" == ";}
	|	NEQ               {$place=" != ";}
	|	NB                {$place=" <= ";}
	|	SMALL             {$place=" < ";}
	|	NS                {$place=" >= ";}
	|	GRAT			{$place=" > ";}
	;
addop   returns [String place]	
        :	ADDOPA             {$place= " + ";}
	|	ADDOPB             {$place= " - ";}
	|	OR                 {$place= " || ";}
	;
mulop	returns [String place]
	:	MULOPA		{$place="*";}
	|	MULOPB		{$place="/";}
	|	DIV		{$place="/";}
	|	MOD		{$place="\%";}
	|	AND		{$place="&&";}
	;		
sign    returns [String place]
	:	'+'               {$place="+";}
	|	'-'               {$place="-";}
	;     		                

PROGRAM	:	'program'|'Program'|'PROGRAM';
VAR	:	'var'|'VAR'|'Var';
ARRAY	:	'array'|'ARRAY'|'Array';
OF	:	'of'|'OF'|'Of';
FUNCTION:	'function'|'FUNCTON'|'Funtion';
PROCEDURE
	:	'procedure'|'PROCEDURE'|'Procedure';	
BEGIN	:	('BEGIN'|'Begin'|'begin');
END	:	('END'|'End'|'end');
IF	:	'IF'|'If'|'if';
THEN	:	'THEN'|'Then'|'then';
ELSE	:	'ELSE'|'Else'|'else';
WHILE	:	'WHILE'|'While'|'while';
DO	:	'DO'|'Do'|'do';
NOT	:	'NOT'|'Not'|'not';
EQUAL	:	'=';
NEQ	:	'<>';
SMALL	:	'<';
GRAT	:	'>';
NB	:	'<=';
NS	:	'>=';
ADDOPA	:	'+';
ADDOPB	:	'-';
OR	:	'OR'|'Or'|'or';
MULOPA	:	'*';
MULOPB	:	'/';
DIV	:	'DIV'|'Div'|'div';
MOD	:	'mod'|'MOD'|'Mod';
AND	:	'AND'|'And'|'and';
ASSIGNOP:	':=';
INTEGER	:	'INTEGER'|'Integer'|'integer';
REAL	:	'REAL'|'Real'|'real';
SEPARATOR
	:	(' '|'\t'|NEWLINE)+;
NEWLINE	:	'\r'? '\n' ;
NUM	:	DIGITS OPTIONAL_FRACTION OPTIONAL_EXPONENT;
OPTIONAL_FRACTION
	:	'.'DIGITS|;
OPTIONAL_EXPONENT
	:	('E' ('+'|'-'|)DIGITS)|;
ID	:	LETTER(LETTER|DIGIT)*;	
DIGITS	:	DIGIT DIGIT*;
LETTER	:	('a'..'z'|'A'..'Z') ;
DIGIT	:	'0'..'9';
WS
  : ( ' '
    | '\t'
    | '\f'

    // handle newlines
    | ( '\r\n'  // DOS/Windows
      | '\r'    // Macintosh
      | '\n'    // Unix
      )
      // increment the line count in the scanner
      //{ newline(); }
    )
    { skip(); }
  ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -