Skip to main content

Posts

Showing posts from September, 2011

Program in C to Find 'N' Queens on an "N x N" Board

A.K.A - " N -Queens Problem" (Note: I computed till 15. N-Queens is only defined for N>= 4)   Manipulate the constant named "MAX"   Program:   #include<stdio.h> #include<conio.h> #define MAX 8 int q [ MAX ] = { 0 }; void printem () { int i , j , k ; for ( i = 0 ; i < MAX ; i ++) { printf ( "\n\t" ); for ( j = q [ i ], k = MAX - j ; j > 0 ; j --) { printf ( "-" ); } printf ( "X" ); for (; k > 0 ; k --) { printf ( "-" ); } }//Outer for ends } int dist ( int x , int y ) { if ( x > y ) return ( x - y ); return ( y - x ); } int match ( int i , int j ) { int k , xi , xj ; for ( k = 0 ; k < i ; k ++) { xi = k ; xj = q [ k ]; if ( xi == i || xj == j ) return

A LEX and Yacc program that recognizes a specific format

A LEX and Yacc program that recognizes a specific format Format: A^n B^n     Lex Program : % option noyywrap %{ #include "y.tab.h" %} %% a return A ; b return B ; \ n return ( '\n' ); . return ; %% Yacc Program : %{ #include<stdio.h> %} % token A B %% statement : anbn '\n' { printf ( "\n Its a valid string!!!" ); return 0 ; } anbn : A B | A anbn B ; %% main () { printf ( "\n Enter some Valid String: " ); yyparse (); } yyerror ( char * s ) { printf ( "\n Its not in anbn" ); }

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d

3. A Lex Program to Count Number of Identifiers in a C program.

A Lex Program to Count Number of Identifiers in a C program. Note: Only type : int/float/double/char are supported. Program: % option noyywrap %{ int id_cnt = 0 ; char ch ; %} %% "int" | "float" | "double" | "char" { ch = input (); for (;;) { if ( ch == ',' ) id_cnt ++; else if ( ch == ';' ) break ; ch = input (); } id_cnt ++; } %% main ( int argc , char ** argv ) { FILE * fp ; fp = fopen ( argv [ 1 ], "r" ); yyin = fp ; yylex (); printf ( "\n total number of identifiers are %d" , id_cnt ); }

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }

1. Lex program to Describe the present operators in a arithmetic statement.

Lex program to Describe the present operators in a arithmetic statement. Note: only +,-,/,* Supported. Program: % option noyywrap %{ %} %% "=" { printf ( "\n operator is Equal" );} "+" { printf ( "\n Operator is Plus" );} "-" { printf ( "\n Operator is minus" );} "/" { printf ( "\n Operator is Division" );} "*" { printf ( "\n Opertor is multiplication" );} [ a - zA - z ]*[ 0 - 9 ]* { printf ( "\n Identifier is %s" , yytext );} . return yytext [ 0 ]; \ n return 0 ; %% main () { yylex (); }