Skip to content

Instantly share code, notes, and snippets.

@Eyakub
Last active October 25, 2017 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eyakub/5a23894f3542a61dd167d6643fa498be to your computer and use it in GitHub Desktop.
Save Eyakub/5a23894f3542a61dd167d6643fa498be to your computer and use it in GitHub Desktop.
It's our Compiler Design lab practice code.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
//scanf("%d %d", &a, &b);
if(a >= 0 && a <= 9 && b >= 0 && b <= 9){
cout << "Sum: " << a + b << endl;
cout << "Sub: " << a - b << endl;
cout << "Mul: " << a * b << endl;
if(b == 0){
cout << "Can't divide with zero" << endl;
}
else{
double res = (double)a / (double)b;
cout << "Div: " << res << endl;
}
}
else{
cout << "Error" << endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char str[100], str2[100];
cout << "Enter first string: ";
cin >> str;
cout << "Enter 2nd string: ";
cin >> str2;
int flag;
for(int i = 0; str1[i] != '\0'; i++){
if(str[i] == str2[i]){
flag = 0;
}
else{
flag = 1;
break;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ch[100];
int len;
cout << "Enter a string: " << endl;
//getting string input
cin.getline(ch, sizeof(ch));
//getting the length of a string
for(len = 0; ch[len] != '\0'; len++);
int space = 0;
for(int i = 0; i != len; i++){
if(ch[i] == ' '){
space++;
}
}
cout << "Length of strign: " << len << endl;
cout << "Space of that string: " << space << endl;
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char ch[100];
int i = 2, j, flag = 0;
printf("Enter a string to check comment or not: ");
gets(ch);
int len = strlen(ch);
for(j = 0; j <= len; j++){
if(ch[j] == '/'){
if(ch[j+1] == '/'){
printf("It's a line comment!\n");
flag = 1;
return;
}
else if(ch[j+1] == '*'){
for(i = j+2; i <= len-1; i++){
if(ch[i] == '*' & ch[i+1] == '/'){
printf("It's multi-line comment!");
flag = 1;
return;
}
else{
continue;
}
}
}
}
if(ch[j] != '/'){
flag = 0;
continue;
}
else if(flag == 0){
printf("it's not a comment\n");
}
}
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char expr[100], operators[20]; //Arrays for storing expression and operator
char variables[20][20]; // 2d array for storing variable or identifier
int constants[20], ascii[100]={0}; //for storing constants, ascii values of expression
int cnt , i, number = 0; //cnt to store string length
int j = -1, k = -1, l= -1 ,n = 0; //counters for filling arrays
int pr[10] = {0}, m = 0; //counters for printing identifier arrays
printf("\n/t/tLEXICAL ANALYZER\n\n");
printf("Enter the String\n\n"); // Accepts expression from user
scanf ("%[^\n]%*c", expr); // "%[^\n]%*c" allows user to include spaces in entered string
cnt = strlen(expr);
printf("\nString length is: %d\n", cnt); //(OPTIONAL)To display string length
for (i = 0; i < cnt; ++i) //Loop to store ascii values in array ascii
{
ascii[i] = (int)expr[i];
}
for(i=0;i<cnt;++i)
{
if( isdigit(expr[i]) ) // Condition for current element to be digit
{
while(isdigit(expr[i])) //Run loop until successive elements are digits
{
number = 10 * number + ascii[i] -'0'; // Needed to convert for eg. '3' '4' to 34
i++;
}
j++;
constants[j] = number;
number = 0;
}
if( isalpha(expr[i]) ) //Condition for current element to be a variable
{
while( isalpha(expr[i]) || isdigit(expr[i]) || expr[i] == '_' ) //Run loop till next element is a letter or digit.
{
k++;
variables[m][k] = expr[i];
i++;
}
m++;
pr[n] = k;
n++;
k = -1;
}
if(expr[i] =='+' || expr[i] =='-' || expr[i] == '/' || expr[i] == '*' || expr[i] =='=' || expr[i] == '^') // Conditions to check for operators
{
l++;
operators[l]= expr[i];
}
}
//——————–Printing the Literals——————————-//
printf("\nThe literals are: \n\n");
for(i=0;i<=j;i++)
{
printf("\tlit%d\t%d \n",i+1,constants[i]);
}
//——————–Printing the Operators——————————-//
printf("\nThe operators are: \n\n");
for(i=0;i<=l;i++)
{
printf("\top%d\t%c\n",i+1,operators[i]);
}
//——————–Printing the Varibles——————————-//
printf("\nThe variables are: \n\n");
for(i=0;i<m;i++)
{
printf("\n\tid%d\t",i+1);
for(j=0;j<=pr[i];j++)
{
printf("%c",variables[i][j]);
}
}
}
My practice work that i'm solving using c or cpp or java though i'm not uploading every code here. :p
#include<stdio.h>
#include<string.h>
int main()
{
char ch[100];
scanf("%[^\n]%*c", ch);
int i = 0;
while(i < strlen(ch)){
printf("%c = %d\n", ch[i], ch[i]);
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment