CSE340Spring2009HW1_Solutions

# CSE340Spring2009HW1_Solutions - Arizona State University...

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

Arizona State University Fulton School of Engineering Computer Science and Engineering CSE 340 Fall 2009 Homework 1 Solution Chapter 4 4.12 expr -> expr + term | term term -> term * factor | term % factor | factor factor -> base ^ factor | base base -> ( expr ) | number number -> number digit | digit digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 4.22 This eliminates the possibility of having expressions with many terms added together. For example, it will be impossible to generate 2+3+4 using this grammar. In fact the only way to parse this input is to use term+term, so either the 2 matches a term and 3+4 matches the other term or 2+3 matches a term and 4 matches the other term. Both cases are not possible because the rule for term has no addition operator. 4.41 a. Consider the number 11.11 which is of the form number 1 . number 2 . The value for number1 is 11 (eleven). The value associated with the other number is1/10+1/100. Since the rule for numbers is of the form number -> number digit | digit it is easy to calculate the value of number 1 because for each added digit we multiple the current value by 10 and add the digit. This is not the case for number 2 because the value associated with a new digit depends on it position and that information is not available when we parse from left to right. When a scanner recognizes the number, we get a string that we could process from one end to get the integer value and from the other end to get the fractional value. b. The new rule looks like frac_number -> digit frac_number | digit number -> number digit | digit decimal_number -> number [.frac_number]

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

View Full Document
This is clearly better for the fractional part because we can express the value of the fractional part as vf(number) = v(digit)/10+vf(number)/10 where vf stands for the fractional value and v(digit) is the value of a digit. So, the value of the 11 that is in the fractional part is v(10)/10+ vf(1)/10 = 0.1 +0.1/10 = 0.11 For the integer part of the number, we can express the decimal value vd as vd(number) = vd(number)*10+v(digit) c) 4.42 a) expr-> ( list ) | a list-> expr { , expr } b)
c) #include <stdio.h> #include <stdlib.h> #include <ctype.h> int token; void expr(void); void list(void); void match(char); void getToken(void) { token = getChar(); } void error(void) { printf("\nAn error occured on input.\n"); } void expr (void)

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

View Full Document
{ switch(token) { case 'a': match('a'); break; case '(': match('('); list(); match(')'); break; default: error(); break; } } void list (void) { expr (); while (token == ‘,’ ) { match (‘,’) ; expr () ; } } void match(char c) { if (token == c) getToken(); else error(); } int command(void)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 22

CSE340Spring2009HW1_Solutions - Arizona State University...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online