SlideShare a Scribd company logo
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;};
Ad

More Related Content

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

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
Eric Poe
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
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
amrit47
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin Yo-An
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
RithikRaj25
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
Ara Pehlivanian
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
Federico Damián Lozada Mosto
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Php functions
Php functionsPhp functions
Php functions
JIGAR MAKHIJA
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
Norhisyam Dasuki
 
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
gertrudebellgrove
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
Chris Chubb
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
Websecurify
 
Stack prgs
Stack prgsStack prgs
Stack prgs
Ssankett Negi
 

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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
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.docx
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
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
priestmanmable
 
Ad

Recently uploaded (20)

Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Ad

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;};
  翻译: