0% found this document useful (0 votes)
5 views

Basic Programming Grammar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic Programming Grammar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

grammar BasicProgramming;

// Parser Rules
program
: statement+ EOF
;

statement
: variableDeclaration
| assignment
| ifStatement
| whileStatement
| printStatement
| readStatement // Nueva regla para lectura
| block
;

variableDeclaration
: TYPE IDENTIFIER ('=' expression)? ';'
;

assignment
: IDENTIFIER '=' expression ';'
;

ifStatement
: 'if' '(' condition ')' block ('else' block)?
;

whileStatement
: 'while' '(' condition ')' block
;

printStatement
: 'print' expression ';'
;

readStatement
: 'read' '(' TYPE ')' IDENTIFIER ';' // Nueva regla para lectura con tipo
;

block
: '{' statement* '}'
;

condition
: expression comparison expression
;

expression
: term (('+' | '-') term)*
;

term
: factor (('*' | '/') factor)*
;

factor
: NUMBER
| IDENTIFIER
| '(' expression ')'
| readExpression // Nueva regla para usar read en expresiones
;

readExpression
: 'read' '(' TYPE ')'
;

comparison
: '=='
| '!='
| '<'
| '>'
| '<='
| '>='
;

// Lexer Rules
TYPE: 'int' | 'float' | 'string';
IDENTIFIER: [a-zA-Z][a-zA-Z0-9]*;
NUMBER: [0-9]+ ('.' [0-9]+)?;
WS: [ \t\r\n]+ -> skip;
COMMENT: '//' ~[\r\n]* -> skip;

You might also like