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

Grammar File

The document defines the grammar for a programming language, specifying rules for program structure, types, variables, expressions, statements, procedures and functions. It outlines the basic building blocks like identifiers, constants, operators, and control structures. Numerous syntactic categories are defined through recursive rules to describe the valid constructs in the language.

Uploaded by

ashokgadde8978
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)
17 views

Grammar File

The document defines the grammar for a programming language, specifying rules for program structure, types, variables, expressions, statements, procedures and functions. It outlines the basic building blocks like identifiers, constants, operators, and control structures. Numerous syntactic categories are defined through recursive rules to describe the valid constructs in the language.

Uploaded by

ashokgadde8978
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/ 19

grammar hello;

options { caseInsensitive = true; }

program
: programHeading optionalInterface block DOT EOF
;
optionalInterface : INTERFACE | ;

programHeading
: PROGRAM identifier optionallparenrparenprogramheading SEMI
| UNIT identifier SEMI
;
optionallparenrparenprogramheading : LPAREN identifierList RPAREN | ;

identifier
: IDENT
;

block
: occurencesOfBlock compoundStatement
;
occurencesOfBlock : availableOccurences occurencesOfBlock | ;
availableOccurences : labelDeclarationPart | constructorCall |
constantDefinitionPart | typeDefinitionPart | variableDeclarationPart |
procedureAndFunctionDeclarationPart | usesUnitsPart | IMPLEMENTATION;

usesUnitsPart
: USES identifierList SEMI
;

classDeclaration:
classForwardDeclaration | classDefinition END SEMI | classDefinition;

classForwardDeclaration:
CLASS;

classDefinition:
optionalPacked CLASS optionalAbstractSealed optionalComponentList;

optionalComponentList:
componentList| ;

componentList:
optionalVisibilitySpecifier optionalFieldDefinition optionalDeclaration;

optionalVisibilitySpecifier:
visibilitySpecifier | ;

optionalFieldDefinition:
fieldDefinition optionalFieldDefinitionRest | ;

optionalFieldDefinitionRest:
fieldDefinition optionalFieldDefinitionRest | ;
optionalDeclaration:
constructorDeclaration | ;

constructorCall : CONSTRUCTOR IDENT DOT IDENT LPAREN parameterCallList RPAREN SEMI


;

parameterCallList : IDENT COLON type_;

visibilitySpecifier:
root1 | PUBLIC | PUBLISHED;

root1:
optionalStrict privateorprotected;

optionalStrict:
STRICT | ;

privateorprotected:
PRIVATE | PROTECTED;

fieldDefinition:
identifierList COLON type_ optionalStatic;

optionalStatic:
STATIC | ;

optionalAbstractSealed:
ABSTRACT | SEALED | ;

optionalPacked:
PACKED | ;

labelDeclarationPart
: LABEL label commaLabelDeclaration SEMI
;

commaLabelDeclaration : COMMA label commaLabelDeclaration | ;

label
: unsignedInteger
;

constantDefinitionPart
: CONST constDefinitionSemi
;
constDefinitionSemi : constantDefinition SEMI nextPartConstDefimictionSemi;
nextPartConstDefimictionSemi : constDefinitionSemi | ;

constantDefinition
: identifier EQUAL constant
;

constantChr
: CHR LPAREN unsignedInteger RPAREN
;
constant
: unsignedNumber
| sign unsignedNumber
| identifier
| sign identifier
| string
| constantChr
;

unsignedNumber
: unsignedInteger
| unsignedReal
;

unsignedInteger
: NUM_INT
;

unsignedReal
: NUM_REAL
;

sign
: PLUS
| MINUS
;

bool_
: TRUE
| FALSE
;

string
: STRING_LITERAL
;

typeDefinitionPart
: TYPE nextPartTypeDefinition
;

nextPartTypeDefinition : typeDefinition nextnextPartType;

nextnextPartType : SEMI nextNextPartTypeDefinition | ;

nextNextPartTypeDefinition : nextPartTypeDefinition | ;

typeDefinition
: identifier EQUAL optionalTypeDefinition
;
optionalTypeDefinition : type_ | functionType | procedureType | classDeclaration
| ;

functionType
: FUNCTION optionalParamList COLON resultType
;
optionalParamList : formalParameterList | ;

procedureType
: PROCEDURE optionalFormalParamList SEMI
;
optionalFormalParamList : formalParameterList | ;

type_//
: simpleType
| structuredType
| pointerType
;

simpleType//
: scalarType
| subrangeType
| typeIdentifier
| stringtype
;

scalarType
: LPAREN identifierList RPAREN
;

subrangeType
: constant DOTDOT constant
;

typeIdentifier
: identifier
| typeIdentifierList
| arrayType
;
typeIdentifierList : CHAR | BOOLEAN | INTEGER | REAL | STRING;

structuredType//
: PACKED unpackedStructuredType
| unpackedStructuredType
;

unpackedStructuredType//
: arrayType
| recordType
| setType
| fileType
;

stringtype
: STRING LBRACK stringTypeList RBRACK
;

stringTypeList : identifier | unsignedNumber ;

arrayType//
: ARRAY LBRACK typeList RBRACK OF componentType
| ARRAY LBRACK2 typeList RBRACK2 OF componentType
| ARRAY OF arrayType componentType
|
;

typeList
: indexType nextTypeList
;
nextTypeList : COMMA indexType nextTypeList | ;

indexType
: simpleType
;

componentType
: type_
;

recordType
: RECORD optionalFieldList END//
;
optionalFieldList : fieldList | ;//

fieldList
: fixedPart optionalSemiVariantPart//
| variantPart
;

optionalSemiVariantPart : SEMI variantPart | ;

fixedPart
: recordSection semiRecordSection//
;
semiRecordSection : SEMI recordSection | ;

recordSection
: identifierList COLON type_ SEMI recordSection
|
;

variantPart
: CASE tag OF variant semiVariant
;

semiVariant : SEMI variant semiVariant | ;

tag
: identifier COLON typeIdentifier
| typeIdentifier
;

variant
: constList COLON LPAREN fieldList RPAREN
;

setType
: SET OF baseType
;

baseType
: simpleType
;

fileType
: FILE OF type_
| FILE
;

pointerType
: POINTER typeIdentifier
;

variableDeclarationPart//
: VAR variableDeclaration semiVariableDeclaration extended SEMI
;

extended:
COMMA variableDeclarationPart | ;

semiVariableDeclaration : SEMI variableDeclaration semiVariableDeclaration | ;

variableDeclaration//
: identifierList COLON type_
;

procedureAndFunctionDeclarationPart
: procedureOrFunctionDeclaration SEMI
;

procedureOrFunctionDeclaration
: procedureDeclaration
| functionDeclaration
| constructorDeclaration
;

procedureDeclaration
: PROCEDURE identifier optionalFormalParamaterList SEMI block
;

constructorDeclaration
: CONSTRUCTOR identifier optionalFormalParamaterList SEMI block
;
optionalFormalParamaterList : formalParameterList optionalFormalParamaterList | ;

formalParameterList
: LPAREN formalParameterSection semiFormalParameterSelection RPAREN
;
semiFormalParameterSelection : SEMI formalParameterSection
semiFormalParameterSelection | ;

formalParameterSection
: parameterGroup
| VAR parameterGroup
| FUNCTION parameterGroup
| PROCEDURE parameterGroup
|
;

parameterGroup
: identifierList COLON typeIdentifier
;

identifierList
: identifier commaIdentifier
;
commaIdentifier : COMMA identifier commaIdentifier | ;
constList
: constant commaConstant
;
commaConstant : COMMA constant commaConstant | ;

functionDeclaration
: FUNCTION identifier optionalFormalParameterList COLON resultType SEMI block
;
optionalFormalParameterList : formalParameterList optionalFormalParameterList | ;

resultType
: typeIdentifier
;

statement
: label COLON unlabelledStatement
| unlabelledStatement
;

unlabelledStatement
: simpleStatement
| structuredStatement
;

simpleStatement
: assignmentStatement
| procedureStatement
| gotoStatement
| emptyStatement_
;

assignmentStatement
: variable ASSIGN expression
;

variable
: atidentifier variableTypeStar
;
variableTypeStar : variablesList variableTypeStar | ;

variablesList : LBRACK expression commaexpression RBRACK | LBRACK2 expression


commaexpression RBRACK2 | DOT identifier | POINTER;
atidentifier : AT identifier | identifier;
commaexpression : COMMA expression commaexpression | ;

expression
: simpleExpression optionalRelationalOperation
;
optionalRelationalOperation : relationaloperator expression | ;

relationaloperator
: EQUAL
| NOT_EQUAL
| LT
| LE
| GE
| GT
| IN
;

simpleExpression
: term optionalAdditiveOperation
;
optionalAdditiveOperation : additiveoperator simpleExpression | ;

additiveoperator
: PLUS
| MINUS
| OR
;

term
: signedFactor optionalMultiplicative
;
optionalMultiplicative : multiplicativeoperator term | ;

multiplicativeoperator
: STAR
| SLASH
| DIV
| MOD
| AND
;

signedFactor
: signs factor
;
signs : PLUS | MINUS | ;

factor
: variable
| LPAREN expression RPAREN
| functionDesignator
| unsignedConstant
| set_
| NOT factor
| bool_
|
;

unsignedConstant
: unsignedNumber
| constantChr
| string
| NIL
;

exceptions : tryExcept | tryFinally;


tryExcept : TRY statements EXCEPT exceptionHandlers END ;

exceptionHandlers : | exception postExceptionHandlers | statements ;

postExceptionHandlers : | ELSE statements ;

exception : ON IDENT COLON IDENT DO statements ;

tryFinally : TRY statements FINALLY statements END SEMI;

functionDesignator
: identifier LPAREN parameterList RPAREN
;

parameterList
: actualParameter commaActualParameterList
;
commaActualParameterList : COMMA actualParameter commaActualParameterList | ;

set_
: LBRACK elementList RBRACK
| LBRACK2 elementList RBRACK2
;

elementList
: element commaElementList
|
;
commaElementList : COMMA element commaElementList | ;

element
: expression optionalDotDotExpression
;
optionalDotDotExpression : DOTDOT expression;

procedureStatement
: identifier optionalLListP
;
optionalLListP : LPAREN parameterList RPAREN | ;

actualParameter
: expression starparameters
;
starparameters : parameterwidth starparameters | ;

parameterwidth
: COLON expression
;

gotoStatement
: GOTO label
;

emptyStatement_
:
;

empty_
:
/* empty */
;

structuredStatement
: compoundStatement
| conditionalStatement
| repetetiveStatement
| withStatement
;

compoundStatement
: BEGIN statements END |
;

statements
: statement semiStatementList
;
semiStatementList : SEMI statement semiStatementList | ;

conditionalStatement
: ifStatement
| caseStatement
;

ifStatement
: IF expression THEN statement colonElseStmt
;
colonElseStmt : ELSE statement | ;

caseStatement
: CASE expression OF caseListElement semiCaseList semiElseStmt END
;
semiCaseList : SEMI caseListElement semiCaseList | ;

semiElseStmt : SEMI ELSE statements | ;

caseListElement
: constList COLON statement
;

repetetiveStatement
: whileStatement
| repeatStatement
| forStatement
;
whileStatement
: WHILE expression DO statement
;

repeatStatement
: REPEAT statements UNTIL expression
;

forStatement
: FOR identifier ASSIGN forList DO statement
;

forList
: initialValue toDownto finalValue
;
toDownto : TO | DOWNTO ;

initialValue
: expression
;

finalValue
: expression
;

withStatement
: WITH recordVariableList DO statement
;

recordVariableList
: variable commaVariable
;
commaVariable : COMMA variable commaVariable | ;

ABSTRACT
: 'ABSTRACT'
;

AND
: 'AND'
;

ARRAY
: 'ARRAY'
;

BEGIN
: 'BEGIN'
;

BOOLEAN
: 'BOOLEAN'
;

CASE
: 'CASE'
;

CHAR
: 'CHAR'
;

CHR
: 'CHR'
;

CLASS
: 'CLASS'
;

CONST
: 'CONST'
;

CONSTRUCTOR
: 'CONSTRUCTOR'
;

DESTRUCTOR
: 'DESTRUCTOR'
;

DIV
: 'DIV'
;

DO
: 'DO'
;

DOWNTO
: 'DOWNTO'
;

DYNAMIC
: 'DYNAMIC';

ELSE
: 'ELSE'
;

END
: 'END'
;

EXCEPT
: 'EXCEPT'
;

FILE
: 'FILE'
;

FINALLY
:'FINALLY'
;

FOR
: 'FOR'
;

FUNCTION
: 'FUNCTION'
;

GOTO
: 'GOTO'
;

IF
: 'IF'
;

IN
: 'IN'
;

INTEGER
: 'INTEGER'
;

LABEL
: 'LABEL'
;

MESSAGE
: 'MESSAGE'
;

MOD
: 'MOD'
;

NIL
: 'NIL'
;

NOT
: 'NOT'
;

OBJECT
: 'OBJECT'
;

OF
: 'OF'
;

ON
:'ON'
;

OR
: 'OR'
;

OVERRIDE
: 'OVERRIDE'
;

PACKED
: 'PACKED'
;

PRIVATE
: 'PRIVATE'
;

PROCEDURE
: 'PROCEDURE'
;

PROGRAM
: 'PROGRAM'
;

PROTECTED
: 'PROTECTED'
;

PUBLIC
: 'PUBLIC'
;
PUBLISHED: 'PUBLISHED';
REAL
: 'REAL'
;

RECORD
: 'RECORD'
;

REPEAT
: 'REPEAT'
;

SEALED
: 'SEALED'
;

SELF
:'SELF'
;

SET
: 'SET'
;

STATIC
: 'STATIC'
;
STRICT: 'STRICT';
THEN
: 'THEN'
;

TO
: 'TO'
;

TYPE
: 'TYPE'
;

UNTIL
: 'UNTIL'
;

VAR
: 'VAR'
;

VIRTUAL
: 'VIRTUAL'
;
WHILE
: 'WHILE'
;

WITH
: 'WITH'
;

PLUS
: '+'
;

MINUS
: '-'
;

STAR
: '*'
;

SLASH
: '/'
;

ASSIGN
: ':='
;

COMMA
: ','
;

SEMI
: ';'
;

COLON
: ':'
;

EQUAL
: '='
;

NOT_EQUAL
: '<>'
;
LT
: '<'
;

LE
: '<='
;

GE
: '>='
;

GT
: '>'
;

LPAREN
: '('
;

RPAREN
: ')'
;

LBRACK
: '['
;

LBRACK2
: '(.'
;

RBRACK
: ']'
;

RBRACK2
: '.)'
;

POINTER
: '^'
;

AT
: '@'
;
DOT
: '.'
;

DOTDOT
: '..'
;

LCURLY
: '{'
;

RCURLY
: '}'
;

UNIT
: 'UNIT'
;

INTERFACE
: 'INTERFACE'
;

USES
: 'USES'
;

STRING
: 'STRING'
;

IMPLEMENTATION
: 'IMPLEMENTATION'
;

TRUE
: 'TRUE'
;

TRY
:'TRY'
;

FALSE
: 'FALSE'
;
WS
: [ \t\r\n] -> skip
;

COMMENT_1
: '(*' .*? '*)' -> skip
;

COMMENT_2
: '{' .*? '}' -> skip
;

IDENT
: ('A' .. 'Z') ('A' .. 'Z' | '0' .. '9' | '_')*
;

STRING_LITERAL
: '\'' ('\'\'' | ~ ('\''))* '\''
;

NUM_INT
: ('0' .. '9') +
| '#'('0' .. '9')+
;

NUM_REAL
: ('0' .. '9') + (('.' ('0' .. '9') + (EXPONENT)?)? | EXPONENT)
;

fragment EXPONENT
: ('E') ('+' | '-')? ('0' .. '9') +
;

You might also like