[[lexer_5]]
 

http://kmlinux.fjfi.cvut.cz/~culik/zpro/lexer05

soubor list.h

class List
{  
...
    void print (int level = 0);
 
    void section (ostream & f, int level);
    void html (ostream & f);
...
};
 
class Item
{
public:
    string name;
    string info;
    List inner;
...
};

soubor list.cpp

void Indent(int level)
{
    if (level > 0)
        cout << string(level * 4,' ');
}
 
void List::print (int level)
{
   // #include <iostream>
   Indent(level);
   cout << "[" << endl;
 
   Item * p = first;
   while (p != nullptr)
   {
       Indent(level);
       cout << "  " << p->name << " " << p->info << endl;
       if (p->inner.first != nullptr)
            p->inner.print(level + 1);
       p = p->next;
   }
 
   Indent(level);
   cout << "]" << endl;
}
 
void indent (ostream & f, int level)
{
    if (level > 0)
        f << string (level, ' ');
}
 
void List::section (ostream & f, int level)
{
   indent (f, 4*level);
   f << "<ul>" << endl;
 
   Item * p = first;
   while (p != nullptr)
   {
       indent (f, 4*level+2);
       f << "<li>";
       f << p->name;
       f << "</li>";
 
       if (p->info != "")
       {
           f << "<img src=\"";
           f << p->info;
           f << "\" height=160 />";
       }
 
       f << endl;
 
       if (p->inner.first != nullptr)
       {
            p->inner.section (f, level + 1);
       }
       p = p->next;
   }
 
   indent (f, 4*level);
   f << "</ul>" << endl;
}
 
void List::html (ostream & f)
{
    f << "<!DOCTYPE html>" << endl;
    f << "<html>" << endl;
    f << "<head>" << endl;
    f << "<title>Vystupni soubor </title>" << endl;
    f << "</head>" << endl;
    f << "<body>" << endl;
    section (f, 1);
    f << "</body>" << endl;
    f << "</html>" << endl;
 
}

soubor lexer.cpp

...
const char quote = '"';
...
else if (ch == quote)
    {
        kind = text;
        nextChar (); // skip quote
        while (ch != quote && ch != zero)
        {
            token = token + ch;
            nextChar ();
        }
        nextChar (); // skip quote
    }
...

soubor main.cpp

#include <iostream>
#include "lexer.h"
#include "list.h"
 
using namespace std;
 
void ctiSeznam (Lexer & f, List & v);
 
void ctiPrvek (Lexer & f, List & v)
{
    string name = f.readIdent ();
    Item * p = new Item (name);
    v.linkLast (p);
 
    if (f.kind == text)
    {
        p->info = f.token;
        f.nextToken();
    }
 
    if (f.isSeparator('{'))
        ctiSeznam (f, p->inner);
}
 
void ctiSeznam (Lexer & f, List & v)
{
    f.checkSeparator('{');
    if (! f.isSeparator('}'))
    {
        ctiPrvek (f, v);
        while (f.isSeparator (','))
        {
           f.nextToken (); // skip ','
           ctiPrvek (f, v);
        }
    }
    f.checkSeparator('}');
 
}
 
int main()
{
    List a;
    Lexer f ("../lexer05/abc.txt");
    ctiSeznam (f, a);
    a.print();
 
    a.html (cout);
 
    ofstream g ("../lexer05/output.html");
    a.html (g);
 
    return 0;
}
 
lexer_5.txt · Last modified: 2018/12/13 12:55 by 194.228.68.106
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki