HW2_Key - Chapter#3 Question#2(20 Points A A java class...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter#3 Question #2 (20 Points) A) A java class header definition header statement. {modifier} "class" identifier ["extends" class_name]["implements" interface_name {"," interface_name}]"{"{field_declaration}"}" B) A java method call statrement <obj_name> "." <method_name> "("[<parameter_list>]") ;" | <method_name> "("[<parameter_list>]") ;" C) A C switch statement "switch""("expression")""{" { ("case"expression":") | ("default"":") | statement } D) A C union definition <union_identifer>"{""{"<object_declaration>":""}""}" E) C float literals (decimal_digits"."[decimal_digits][exponent_part][float_type_suffix]) | ("."decimal_digits [exponent_part][float_type_suffix]) | decimal_digits [exponent_part][float_type_suffix]) Question# 3 (20 Points) A) A = A * ( B + ( C * A ) ) <assign> <id> = <expr> A = <expr> A = <id> * <expr> A = A * <expr> A = A * ( <expr> ) A = A * ( <id> + <expr> ) A = A * ( B + <expr> ) A = A * ( B + ( <expr> ) ) A = A * ( B + ( <id> * <expr> ) ) A = A * ( B + ( C * <expr> ) ) A = A * ( B + ( C * <id> ) ) A=A*(B+(C*A)) B) B = C * ( A * C + B ) <assign> <id> = <expr> B = <expr> B = <id> * <expr> A = C * <expr> B = C * ( <expr> ) B = C * ( <id> * <expr> ) B = C * ( A * <expr> ) B = C * ( A * <id> + <expr> ) A = C * ( A * C + <expr> ) A = C * ( A * C + <id> ) A=C*(A* C+B ) C) A = A * ( B + ( C ) ) <assign> <id> = <expr> A = <expr> A = <id> * <expr> A = A * <expr> A = A * ( <expr> ) A = A * ( <id> + <expr> ) A = A * ( B + <expr> ) A = A * ( B + ( <expr> ) ) A = A * ( B + ( <id> ) ) A=A*(B+(C)) Question# 7 (10 Points) The grammar will generate x number of letter a followed by y number of letter b followed by z number of letter c (where x,y,z > 0) Question#9 (10 Points) Statements (a) and (e) can be generated Question#10 (10 Points) A possible grammar will be: <S> a <S> b | ab Chapter #4 Question #2 (10 Points) One possible state diagram is as follows: Note: When decimal digit is found we keep getting the remaining digits (e.g getChar + addChar) we repeat until we find exponent part, float suffix or the (.) then we go to the next state. Question# 5 (10 Points) The following is the JAVA possible implementation for the checking float numbers. Note: The follwing code is a working one but it is not perfect. It needs some slight modification to exactly match the above diagram import java.io.*; public class FloatFinder { public static void main(String args)throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please Enter a floating number"); String number=input.readLine(); int num_length=number.length(); String ch; int count=0,r=0,x=0; boolean minus=false; if((number.charAt(0)+"").equals("-")) { minus=true; r=1; System.out.println(number.charAt(0)+""); } for(int i=r;i<num_length;i++) { ch=number.charAt(i)+""; System.out.print(ch+"\t"); if(ch.compareTo("-")==0) { System.out.println("<-- NOT HERE !"); System.out.println("The number is NOT float"); x=1; break; } if(ch.compareTo(".")==0) { count++; if(count>1) { System.out.println("<-- NOT Again !"); System.out.println("The number is NOT float"); x=1; break; } } if(ch.equals(".")||ch.equals("0")||ch.equals("1")|| ch.equals("2")||ch.equals("3")||ch.equals("4")|| ch.equals("5")||ch.equals("6")||ch.equals("7")|| ch.equals("8")||ch.equals("9")) { System.out.println("<-- Accepted"); } else { System.out.println("<-- Not a float"); System.out.println("The number is NOT float"); x=1; break; } System.out.println(); } if (x!=1) System.out.println(number+" is a float number"); } } Question#7 (10 Points) THE STACK 0 0id5 0F3 0T2 0T2*7 0T2*7(4 0T2*7(4id5 0T2*7(4F3 0T287(4T2 0T2*7(4E8 0T2*7(4E8+6 0T2*7(4E8+6id5 0T2*7(4E8+6f3 0T2*7(4E8+6t9 0T2*7(4E8 0T2*7(4E8)11 0T2*7F10 0T2 0E1 THE INPUT id*(id+id)$ *(id+id)$ *(id+id)$ *(id+id)$ *(id+id)$ id+id)$ +id)$ +id)$ +id)$ +id)$ id)$ )$ )$ )$ )$ $ $ $ $ THE ACTION Shift 5 Reduce6 goto[0,F] Reduce4 goto[0,T] Shift7 Shift4 Shift5 Reduce6 goto[4,F] Reduce4 goto[4,T] Reduce2 goto[4,E] Shift6 Shift5 Reduce6 goto[6,F] Reduce4 goto[6,T] Reduce1 goto[4,E] Shift11 Reduce5 goto[7,F] Reduce3 goto[0,T] Reduce2 goto[0,E] Accept ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online