Tree Walker file
July 28th, 2010 By mir.nauman

It is highly recommended to go through the following links first

  • http://imsciences.edu.pk/serg/2010/07/antlr-introduction/
  • http://imsciences.edu.pk/serg/2010/07/setting-up-antlr-3-1-in-eclipse-3-5-for-windows/
  • http://imsciences.edu.pk/serg/2010/07/grammer-file-syntex-symentics-of-our-policy-writing-with-antlr/

In this grammar file we do our semantic analysis, by walking and evaluating the nodes, of the AST tree, generated in our grammar file.

Figure 1. A screenshot of the tree of a statement.

tree grammar StatementWalker1;

options {

language = Java;

tokenVocab = XL1;

ASTLabelType = CommonTree;

}

@header{

package com.fawad.policywritingtool;

}

policy

: (mode ‘(””‘ appname ‘”‘ ‘as’ app ‘,’ ‘”‘ permname’”‘ ‘as’ perm ‘)’ ‘:’

statement+ ‘->’ policyeffect ‘(’app ‘,’ perm ‘)’ ‘;’ )+

;

mode

: RES

;

appname

: IDENT(’.’ IDENT | ‘_’ IDENT)+

;

app

: IDENT+

;

method

: IDENT+’()’

;

permname

: IDENT(’.’ IDENT | ‘_’ IDENT)+

;

perm

: IDENT+

;


An integer ‘result’ is returned when the statement is evaluated

statement returns [int result] :    e=expression  { result = e; }

;


Following are the operators and their respective implementation in the walker

expression returns [int result]

: ^(’AND’ op1=expression op2=expression ) { if(op1==1 && op2 == 1) result =1; else result =0;}

|^(’OR’ op1=expression op2=expression ) { if(op1==1 || op2 == 1) result =1; else result =0;}

|^(’>’ op1=expression op2=expression) { result= op1 > op2?1:0; }

| ^(’<’ op1=expression op2=expression) { result= op1 < op2?1:0;}

| ^(’>=’ op1=expression op2=expression) { result= op1 >= op2?1:0;}

| ^(’<=’ op1=expression op2=expression) { result= op1 <= op2?1:0;}

| ^(’=’ op1=expression op2=expression) { result= op1 != op2?1:0;}

| ^(’!=’ op1=expression op2=expression) { result= op1 != op2?1:0;}

| ^(’+’ op1=expression op2=expression) { result = op1 + op2; }

| ^(’-’ op1=expression op2=expression) { result = op1 – op2; }

| ^(’*’ op1=expression op2=expression) { result = op1 * op2; }

| ^(’/’ op1=expression op2=expression) { result = op1 / op2; }

| ^(’%’ op1=expression op2=expression) { result = op1 \% op2; }

| ^(NEGATION e=expression) { result = -e;}

| TRUE{ result = 1;}

| FALSE{ result = 0;}

| INTEGER { result= Integer.parseInt($INTEGER.text); }

;

Policy effect returns an integer ‘res’ with the value ‘1′ for allow and  ’0′ for deny

policyeffect returns [int res ]

: ALLOW {res= 1 ;}

| DENY {res= 0 ;}

;

Courtesy of Fawad, Owais, & Mir Nauman Tahir

Leave a Reply


(Required)

(Required)