Code Monkey home page Code Monkey logo

pl's Introduction

PL

2018년 2학기 Programming language 실습과 과제

과제 진행

IntelliJ + ANTLR V4 grammar plugin

IntelliJ에 ANTLR V4 gammar plugin 설치 : 유튜브

ANTLR getting started 문서로 퀵스타트

  1. g4 파일 생성
  2. grammar 작성
    // Define a grammar called Hello
     grammar Hello;
     r  : 'hello' ID ;         // match keyword hello followed by an identifier
     ID : [a-z]+ ;             // match lower-case identifiers
     WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
  3. g4 파일에서 오른쪽 마우스 클릭 - Generate ANTLR Recognizer 선택
  4. g4 파일에서 테스트하고 싶은 룰에서 오른쪽 마우스 클릭 - Test Rule 선택
  5. 아래창에 소스 코드 입력
  6. 파스트리 확인

img img2

Excercise I

Write a grammar that can evaluate 4 basic math operations (+, -, *, /)

grammar Exercise;
prog: (expr NEWLINE)*;
expr: term (('+'|'-') term)*;
term: factor (('*'|'/') factor)*;
factor: '('expr')' | INT;
NEWLINE : [\r\n]+;
INT : [0-9]+;

exercise1

Homework I

Write a grammar for a calculator language

var a, b: integer;
var x: real;

a := 10.002;
b := 2+(4-1)*2;
x := a+b;
  • The language will have a set of statements including declarations, assignments, and expressions.
    • Declarations must come before other statements like C/C++.
    • Each statement must end with a semi-colon.
    • The four basic arithmetical operations (+, -, *, and /) consisting of variables and numbers have to be provided
  • Only positive integer and real numbers (no signed numbers); no scientific notation (e.g., 6E+5)
grammar Homework1;
prog: (def NEWLINE)*(assign NEWLINE)*;
def: 'var ' ID (', ' ID)* ': ' ('integer' | 'real') ';';
assign: ID ' := ' expr ';';
expr: term (('+'|'-') term)* ;
term: factor (('*'|'/') factor)*;
factor: '('expr')' | NUM | ID;
NEWLINE : [\r\n]+;
ID: ([A-Z]|[a-z]|'_')+;
NUM : [0-9]+('.'[0-9]+)?;

hw1 hw2 hw3

pl's People

Contributors

cosmicb0y avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.