In: Computer Science
Very easy: Write an EBNF rule that describes the following for statement in Java
for ( counter = 1; counter <= 10; counter ++)
{
sum=sum+1;
}
Then test your EBNF rule in Java as a recursive-descent subprogram
Java:
import java.util.*; // Import package
public class EBNF // Class declaration
{
public static void main(String args[]) // Main method with void return type
{
StringTokenizer str1 = new StringTokenizer("for(counter= 1; counter<=10; counter++) {sum=sum+1;}", " ");
int count=0; // Value initialization
while (str1.hasMoreTokens())
{
count++; // Count = count + 1
System.out.println("Token "+count+" "+str1.nextToken()); // Print command
}
System.out.println("Total Tokens :: "+count); // Print command
}
}
Output:
Hope this helps.