Catatan

Menunjukkan catatan yang berlabel Compiler

Compiler Design : Example of a grammar rule

Imej
  Refer chatgpt :  https://chatgpt.com/share/f3fa2952-8246-410e-b268-184f4af4a896 Certainly! Let's go through each type of grammar rule with an example. (a) Right Linear Grammar A right linear grammar has rules where the right-hand side consists of a terminal followed by at most one non-terminal, or just a terminal. Example: A → a B A \rightarrow aB A → a B B → b B \rightarrow b B → b Here, each production rule either ends with a terminal or has a terminal followed by a single non-terminal. (b) Context-Free Grammar (CFG), but not Right Linear A context-free grammar has rules where the left-hand side consists of a single non-terminal, but the right-hand side can be any combination of terminals and non-terminals. Example: A → a B b A \rightarrow aBb A → a B b B → c B \rightarrow c B → c This grammar is context-free because each left-hand side is a single non-terminal. However, it is not right linear because the right-hand side of the first rule has non-terminals and terminals not i

Compiler : Finite State Machine code sample

Sample 0 :  import java.io.*; import java.util.*; /* String containing an even number of one     0   1 ------------ A*  A   B B   B   A ------------ */ public class FSM {     final static int STATES=2, INPUTS=2;         public static void main (String[] args) throws IOException{                 boolean [] accept = new boolean[STATES];         int [][] fsm = new int[STATES][INPUTS];         accept[0] = true;         accept[1] = false;                 fsm[0][0] = 0;         fsm[0][1] = 1;         fsm[1][0] = 1;         fsm[1][1] = 0;                 // State A = 0, State B = 1         int inp = 0;         int state = 0;                 try{             inp = System.in.read() - '0';                         while(inp >= 0 && inp < INPUTS){                 state = fsm[state][inp];                 inp = System.in.read() - '0';             }         }         catch(IOException ioe){             System.out.println("IO error " + ioe);         }