SlideShare a Scribd company logo
1 of 17
java compiler/Compiler1.javajava compiler/Compiler1.java
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
publicclassTest1{
publicstaticvoid main(String[] args)throwsRecognitionExceptio
n{
CharStream stream =
newANTLRStringStream("program XLSample1 =rn"+
"/*rn"+
" constant one : Integer := 1;rn"+
" constant two : Integer := 2 * 3;rn"+
" var x, y, z : Integer := 42;rn"+
"*/rn"+
"rn"+
" procedure foo() =rn"+
" var x : Integer := 2;rn"+
" beginrn"+
" end foo.rn"+
" procedure fee(y : Integer) =rn"+
" var x : Integer := 2;rn"+
" beginrn"+
" end fee.rn"+
" function fie(y : Integer) : Integer =rn"+
" var x : Integer := 2;rn"+
" beginrn"+
" return y;rn"+
" end fie.rn"+
"beginrn"+
"end XLSample1.");
SampleLexer lexer =newSampleLexer(stream);
TokenStream tokenStream =newCommonTokenStream(lexer);
SampleParser parser =newSampleParser(tokenStream);
parser.program();
System.out.println("ok");
}
}
java compiler/Compiler2.javajava compiler/Compiler2.java
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
publicclassTest2{
publicstaticvoid main(String[] args)throwsRecognitionExceptio
n{
CharStream stream =
newANTLRStringStream("3 * (2 + 4) * 3");
Sample2Lexer lexer =newSample2Lexer(stream);
TokenStream tokenStream =newCommonTokenStream(lexer);
Sample2Parser parser =newSample2Parser(tokenStream);
int result = parser.evaluator();
System.out.println("ok - result is "+ result);
}
}
java compiler/src1.g4
grammar scr1;
options {
language = Java;
}
@header {
package a.b.c;
}
@lexer::header {
package a.b.c;
}
program
: 'program' IDENT '='
(constant | variable | function | procedure | typeDecl)*
'begin'
statement*
'end' IDENT '.'
;
constant
: 'constant' IDENT ':' type ':=' expression ';'
;
variable
: 'var' IDENT (',' IDENT)* ':' type (':=' expression)? ';'
;
type
: 'Integer'
| 'Boolean'
| 'String'
| 'Char'
| IDENT
| typeSpec
;
typeDecl
: 'type' IDENT '=' typeSpec ';'
;
typeSpec
: arrayType
| recordType
| enumType
;
arrayType
: 'array' '[' INTEGER '..' INTEGER ']' 'of' type
;
recordType
: 'record' field* 'end' 'record'
;
field
: IDENT ':' type ';'
;
enumType
: '<' IDENT (',' IDENT)* '>'
;
statement
: assignmentStatement
| ifStatement
| loopStatement
| whileStatement
| procedureCallStatement
;
procedureCallStatement
: IDENT '(' actualParameters? ')' ';'
;
actualParameters
: expression (',' expression)*
;
ifStatement
: 'if' expression 'then' statement+
('elsif' expression 'then' statement+)*
('else' statement+)?
'end' 'if' ';'
;
assignmentStatement
: IDENT ':=' expression ';'
;
exitStatement
: 'exit' 'when' expression ';'
;
whileStatement
: 'while' expression 'loop'
(statement|exitStatement)*
'end' 'loop' ';'
;
loopStatement
: 'loop' (statement|exitStatement)* 'end' 'loop' ';'
;
returnStatement
: 'return' expression ';'
;
procedure
: 'procedure' IDENT '(' parameters? ')' '='
(constant | variable)*
'begin'
statement*
'end' IDENT '.'
;
function
: 'function' IDENT '(' parameters? ')' ':' type '='
(constant | variable)*
'begin'
(statement|returnStatement)*
'end' IDENT '.'
;
parameters
: parameter (',' parameter)*
;
parameter
: 'var'? IDENT ':' type
;
// expressions -- fun time!
term
: IDENT
| '(' expression ')'
| INTEGER
| STRING_LITERAL
| CHAR_LITERAL
| IDENT '(' actualParameters ')'
;
negation
: 'not'* term
;
unary
: ('+' | '-')* negation
;
mult
: unary (('*' | '/' | 'mod') unary)*
;
add
: mult (('+' | '-') mult)*
;
relation
: add (('=' | '/=' | '<' | '<=' | '>=' | '>') add)*
;
expression
: relation (('and' | 'or') relation)*
;
MULTILINE_COMMENT : '/*' .* '*/' {$channel = HIDDEN;} ;
STRING_LITERAL
: '"'
{ StringBuilder b = new StringBuilder(); }
( '"' '"' { b.appendCodePoint('"');}
| c=~('"'|'r'|'n') { b.appendCodePoint(c);}
)*
'"'
{ setText(b.toString()); }
;
CHAR_LITERAL
: ''' . ''' {setText(getText().substring(1,2));}
;
fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | 't' | 'n' | 'r' | 'f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('n'|'r') {$channel = HIDDEN;};
java compiler/src2.g4
grammar src2;
options {
language = Java;
}
@header {
package a.b.c;
}
@lexer::header {
package a.b.c;
}
evaluator returns [int result]
: expression EOF { $result = $expression.result; }
;
// expressions -- fun time!
term returns [int result]
: IDENT {$result = 0;}
| '(' expression ')' {$result = $expression.result;}
| INTEGER {$result =
Integer.parseInt($INTEGER.text);}
;
unary returns [int result]
: { boolean positive = true; }
('+' | '-' { positive = !positive; })* term
{
$result = $term.result;
if (!positive)
$result = -$result;
}
;
mult returns [int result]
: op1=unary { $result = $op1.result; }
( '*' op2=unary { $result = $result * $op2.result; }
| '/' op2=unary { $result = $result / $op2.result; }
| 'mod' op2=unary { $result = $result %
$op2.result; }
)*
;
expression returns [int result]
: op1=mult { $result = $op1.result; }
( '+' op2=mult { $result = $result + $op2.result; }
| '-' op2=mult { $result = $result - $op2.result; }
)*
;
MULTILINE_COMMENT : '/*' .* '*/' {$channel = HIDDEN;} ;
STRING_LITERAL
: '"'
{ StringBuilder b = new StringBuilder(); }
( '"' '"' { b.appendCodePoint('"');}
| c=~('"'|'r'|'n') { b.appendCodePoint(c);}
)*
'"'
{ setText(b.toString()); }
;
CHAR_LITERAL
: ''' . ''' {setText(getText().substring(1,2));}
;
fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | 't' | 'n' | 'r' | 'f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('n'|'r') {$channel = HIDDEN;};

More Related Content

Similar to java compilerCompiler1.javajava compilerCompiler1.javaimport.docx

PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesEric Poe
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the mastersAra Pehlivanian
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docxgertrudebellgrove
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 TrainingChris Chubb
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.jsWebsecurify
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfMuhammadMaazShaik
 

Similar to java compilerCompiler1.javajava compilerCompiler1.javaimport.docx (20)

C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
7 functions
7  functions7  functions
7 functions
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Php functions
Php functionsPhp functions
Php functions
 
Java operators
Java operatorsJava operators
Java operators
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 

More from priestmanmable

9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docxpriestmanmable
 
a 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxa 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxpriestmanmable
 
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docxpriestmanmable
 
92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docxpriestmanmable
 
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxA ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxpriestmanmable
 
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docxpriestmanmable
 
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docxpriestmanmable
 
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docxpriestmanmable
 
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docxpriestmanmable
 
9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docxpriestmanmable
 
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docxpriestmanmable
 
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docxpriestmanmable
 
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docxpriestmanmable
 
800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docxpriestmanmable
 
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docxpriestmanmable
 
8.0 RESEARCH METHODS These guidelines address postgr.docx
8.0  RESEARCH METHODS  These guidelines address postgr.docx8.0  RESEARCH METHODS  These guidelines address postgr.docx
8.0 RESEARCH METHODS These guidelines address postgr.docxpriestmanmable
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docxpriestmanmable
 
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docxpriestmanmable
 
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docxpriestmanmable
 
8Network Security April 2020FEATUREAre your IT staf.docx
8Network Security  April 2020FEATUREAre your IT staf.docx8Network Security  April 2020FEATUREAre your IT staf.docx
8Network Security April 2020FEATUREAre your IT staf.docxpriestmanmable
 

More from priestmanmable (20)

9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx
 
a 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxa 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docx
 
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
 
92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx
 
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxA ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
 
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
 
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
 
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
 
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
 
9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx
 
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
 
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
 
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
 
800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx
 
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
 
8.0 RESEARCH METHODS These guidelines address postgr.docx
8.0  RESEARCH METHODS  These guidelines address postgr.docx8.0  RESEARCH METHODS  These guidelines address postgr.docx
8.0 RESEARCH METHODS These guidelines address postgr.docx
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx
 
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
 
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
 
8Network Security April 2020FEATUREAre your IT staf.docx
8Network Security  April 2020FEATUREAre your IT staf.docx8Network Security  April 2020FEATUREAre your IT staf.docx
8Network Security April 2020FEATUREAre your IT staf.docx
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

java compilerCompiler1.javajava compilerCompiler1.javaimport.docx

  • 1. java compiler/Compiler1.javajava compiler/Compiler1.java import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CharStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; import org.antlr.runtime.TokenStream; publicclassTest1{ publicstaticvoid main(String[] args)throwsRecognitionExceptio n{ CharStream stream = newANTLRStringStream("program XLSample1 =rn"+ "/*rn"+ " constant one : Integer := 1;rn"+ " constant two : Integer := 2 * 3;rn"+ " var x, y, z : Integer := 42;rn"+ "*/rn"+ "rn"+ " procedure foo() =rn"+ " var x : Integer := 2;rn"+ " beginrn"+ " end foo.rn"+ " procedure fee(y : Integer) =rn"+ " var x : Integer := 2;rn"+ " beginrn"+ " end fee.rn"+ " function fie(y : Integer) : Integer =rn"+ " var x : Integer := 2;rn"+ " beginrn"+ " return y;rn"+ " end fie.rn"+ "beginrn"+ "end XLSample1.");
  • 2. SampleLexer lexer =newSampleLexer(stream); TokenStream tokenStream =newCommonTokenStream(lexer); SampleParser parser =newSampleParser(tokenStream); parser.program(); System.out.println("ok"); } } java compiler/Compiler2.javajava compiler/Compiler2.java import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CharStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; import org.antlr.runtime.TokenStream; publicclassTest2{ publicstaticvoid main(String[] args)throwsRecognitionExceptio n{ CharStream stream = newANTLRStringStream("3 * (2 + 4) * 3"); Sample2Lexer lexer =newSample2Lexer(stream); TokenStream tokenStream =newCommonTokenStream(lexer); Sample2Parser parser =newSample2Parser(tokenStream); int result = parser.evaluator(); System.out.println("ok - result is "+ result); } } java compiler/src1.g4 grammar scr1; options {
  • 3. language = Java; } @header { package a.b.c; } @lexer::header { package a.b.c; } program : 'program' IDENT '=' (constant | variable | function | procedure | typeDecl)* 'begin' statement* 'end' IDENT '.' ;
  • 4. constant : 'constant' IDENT ':' type ':=' expression ';' ; variable : 'var' IDENT (',' IDENT)* ':' type (':=' expression)? ';' ; type : 'Integer' | 'Boolean' | 'String' | 'Char' | IDENT | typeSpec ;
  • 5. typeDecl : 'type' IDENT '=' typeSpec ';' ; typeSpec : arrayType | recordType | enumType ; arrayType : 'array' '[' INTEGER '..' INTEGER ']' 'of' type ; recordType : 'record' field* 'end' 'record' ;
  • 6. field : IDENT ':' type ';' ; enumType : '<' IDENT (',' IDENT)* '>' ; statement : assignmentStatement | ifStatement | loopStatement | whileStatement | procedureCallStatement ; procedureCallStatement : IDENT '(' actualParameters? ')' ';'
  • 7. ; actualParameters : expression (',' expression)* ; ifStatement : 'if' expression 'then' statement+ ('elsif' expression 'then' statement+)* ('else' statement+)? 'end' 'if' ';' ; assignmentStatement : IDENT ':=' expression ';' ; exitStatement
  • 8. : 'exit' 'when' expression ';' ; whileStatement : 'while' expression 'loop' (statement|exitStatement)* 'end' 'loop' ';' ; loopStatement : 'loop' (statement|exitStatement)* 'end' 'loop' ';' ; returnStatement : 'return' expression ';' ; procedure
  • 9. : 'procedure' IDENT '(' parameters? ')' '=' (constant | variable)* 'begin' statement* 'end' IDENT '.' ; function : 'function' IDENT '(' parameters? ')' ':' type '=' (constant | variable)* 'begin' (statement|returnStatement)* 'end' IDENT '.' ; parameters : parameter (',' parameter)* ;
  • 10. parameter : 'var'? IDENT ':' type ; // expressions -- fun time! term : IDENT | '(' expression ')' | INTEGER | STRING_LITERAL | CHAR_LITERAL | IDENT '(' actualParameters ')' ; negation : 'not'* term
  • 11. ; unary : ('+' | '-')* negation ; mult : unary (('*' | '/' | 'mod') unary)* ; add : mult (('+' | '-') mult)* ; relation : add (('=' | '/=' | '<' | '<=' | '>=' | '>') add)* ;
  • 12. expression : relation (('and' | 'or') relation)* ; MULTILINE_COMMENT : '/*' .* '*/' {$channel = HIDDEN;} ; STRING_LITERAL : '"' { StringBuilder b = new StringBuilder(); } ( '"' '"' { b.appendCodePoint('"');} | c=~('"'|'r'|'n') { b.appendCodePoint(c);} )* '"' { setText(b.toString()); } ; CHAR_LITERAL
  • 13. : ''' . ''' {setText(getText().substring(1,2));} ; fragment LETTER : ('a'..'z' | 'A'..'Z') ; fragment DIGIT : '0'..'9'; INTEGER : DIGIT+ ; IDENT : LETTER (LETTER | DIGIT)*; WS : (' ' | 't' | 'n' | 'r' | 'f')+ {$channel = HIDDEN;}; COMMENT : '//' .* ('n'|'r') {$channel = HIDDEN;}; java compiler/src2.g4 grammar src2; options { language = Java; } @header {
  • 14. package a.b.c; } @lexer::header { package a.b.c; } evaluator returns [int result] : expression EOF { $result = $expression.result; } ; // expressions -- fun time! term returns [int result] : IDENT {$result = 0;} | '(' expression ')' {$result = $expression.result;} | INTEGER {$result = Integer.parseInt($INTEGER.text);} ;
  • 15. unary returns [int result] : { boolean positive = true; } ('+' | '-' { positive = !positive; })* term { $result = $term.result; if (!positive) $result = -$result; } ; mult returns [int result] : op1=unary { $result = $op1.result; } ( '*' op2=unary { $result = $result * $op2.result; } | '/' op2=unary { $result = $result / $op2.result; } | 'mod' op2=unary { $result = $result % $op2.result; } )*
  • 16. ; expression returns [int result] : op1=mult { $result = $op1.result; } ( '+' op2=mult { $result = $result + $op2.result; } | '-' op2=mult { $result = $result - $op2.result; } )* ; MULTILINE_COMMENT : '/*' .* '*/' {$channel = HIDDEN;} ; STRING_LITERAL : '"' { StringBuilder b = new StringBuilder(); } ( '"' '"' { b.appendCodePoint('"');} | c=~('"'|'r'|'n') { b.appendCodePoint(c);} )*
  • 17. '"' { setText(b.toString()); } ; CHAR_LITERAL : ''' . ''' {setText(getText().substring(1,2));} ; fragment LETTER : ('a'..'z' | 'A'..'Z') ; fragment DIGIT : '0'..'9'; INTEGER : DIGIT+ ; IDENT : LETTER (LETTER | DIGIT)*; WS : (' ' | 't' | 'n' | 'r' | 'f')+ {$channel = HIDDEN;}; COMMENT : '//' .* ('n'|'r') {$channel = HIDDEN;};