csc402-ln006

# csc402-ln006 - Intermediate Representation(IR Our simple...

This preview shows pages 1–7. Sign up to view the full content.

Intermediate Representation (IR) Our simple, syntax directed interpretation scheme that we worked out for the exp1 language, where we computed values for expressions as soon as we recognized them in the input stream, will fail with more complex languages. Let’s extend exp1 with conditional and unconditional jump instructions and call the language exp1bytecode Please read Chap. 4 in the Patterns Book.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Exp1bytecode Language Design Four new statements: stop ; jumpT exp label ; jumpF exp label ; jump label ; Note : exp is an integer expression and is interpreted as false if its value is zero otherwise it is true The notion of a labeled statement: E.g., lab3: store x (- x 1) ; jumpT x lab3; Two new operators: =, =<, that return 0 when false otherwise they will return 1. Lastly, we also allow for negative integer constants: -2, -12
Exp1bytecode prog : (labelDef? stmt)+ ; labelDef : NAME ':' ; stmt : 'print' exp ';' | 'store' lhsvar exp ';' | 'jumpT' exp label ';' | 'jumpF' exp label ';' | 'jump' label ';' | 'stop' ';' ; exp : '+' exp exp | '-' exp exp | '*' exp exp | '/' exp exp | '=' exp exp | '=<' exp exp | '(' exp ')' | rhsvar | NUM ; label : NAME ; lhsvar : NAME ; rhsvar : NAME ; NAME : ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')* ; NUM : '-'? ('0'..'9')+ ;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Exp1bytecode Here is a simple example program in this language: // this program prints out a // list of integers store x 10 ; L1: print x ; store x (- x 1) ; jumpT x L1 ; stop ; Problem: in syntax directed interpretation all info needs to be available at statement execution time; the label definition is not available at jump time. Answer: we will use an IR to do the actual interpretation.
Syntax directed interpretation exp returns [int value] : '+' e1=exp e2=exp { \$value = \$e1.value + \$e2.value; } | '-' e1=exp e2=exp { \$value = \$e1.value - \$e2.value; } | '(' e=exp ')' { \$value = \$e.value; } | rhsvar { \$value = \$rhsvar.value; } | NUM { \$value = Integer.parseInt(\$NUM.text); } ; lhsvar returns [String name] : NAME { \$name = \$NAME.text; }; rhsvar returns [int value] : NAME { Integer v = (Integer)memory.get(\$NAME.text); if (v == null) { \$value = 0; } else { \$value = v.intValue(); } } ; // lexical rules NAME : ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')* ; NUM : '0'..'9'+ ; In our simple expression interpreter we saw that all the info was available at expression execution.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Syntax directed interpretation prog : ( labelDef? stmt)+ ; labelDef : NAME ':' ; stmt : 'print' exp ';' | 'store' lhsvar exp ';' | 'jumpT' exp label ';' | 'jumpF' exp label ';' | 'jump' label ';' | 'stop' ';' ; But here we see that label definitions are non-local to jump statements and therefore cannot be executed in a syntax directed manner.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern